• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

instanceof

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unclear to distinguish between situations under which the instanceof operator generates a COMPILER ERROR and returns FALSE.
Could someone please throw some light on this issue.
Thanks in Advance.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* You'll get a compiler error if there's just NO way that the object can be an instanace of the class.
* You'll be returned false if at compile-time there's a chance that the object can be an instanceof the given class, but at run-time it turns out that its not...
* You'll be returned true if at compile-time there's a chance that the object can be an instanceof the given class, and at run-time it turns out it is.
Here's a bit of explanation with an example:

Ok... so in line 1 you're trying to see if a is an instance of Cat -- there is a possible relationship -- and the compiler knows that a compile-time (Cat extends Animal -- but it doesn't know at compile-time if this particular Animal object is an instance of the Cat class)... but at run-time, it turns out that it is not an instance of Cat, so it returns false
Same thing happens in line 2 (but with the class Dog)
In line 3 -- we're trying to compare a Cat object to the Dog class. Granted they both are subclasses of Animal -- but there's NO WAY that a Cat can be a Dog. so you get a compile-time error
In line 4 -- the compiler knows at compile time that there's a valid possible relationship between the Cat object and the Animal class -- and at runtime -- its still there it returns true
In line 5, just like in 1, the compiler knows the relationship is possible. And at run-time -- it turns out its there -- it returns true
Does that help??
[ January 04, 2003: Message edited by: Jessica Sant ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jessica,
very good example!
Thank you very much

Sam
 
author
Posts: 8995
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jessica -
Love those animal classes...
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Love those animal classes...
Well.. I was tempted to do

but for those poor poor underprivledged people who don't know what a MINI is... they might have been a tad confused.
[ January 04, 2003: Message edited by: Jessica Sant ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!