Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

cast & instanceof

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is that I can cast a "null" to any class but when i use instanceof for same class it returns false....
i.e..if instanceof returns true then i can cast the variable...but though instanceof returns false for "null instanceof Light" iam able to type cast it
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

instanceof checks if one reference to a class (an object of ClassA) is of the same type as another class (ClassB) or one of its decendant classes.

The comparison is always between an object on the left and a class on the right, and since null is not a class or an object it makes sense that you get false.


Chris
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic