Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question about instanceof

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm relatively new to java and am studying for the SCJP. I haven't seen the instanceof operator in use in any code that I've looked at so far, but what's the point?

If you can only use it to test objects against class types that are in the same class hierarchy, won't it always return true if the variable on the left refers to an object that is in the class hierarchy (or false if null) and always not compile if the object refered to is not? Of course, it will return false if the variable is null, but in that case why not just test for null?

Thanx,
DMad
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this answers your question.
Execute it and observe the results.

 
David Madouros
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very helpful. Let me see if I get it...

It's about polymorphism and storing an object that belongs to a subclass (B extends A) in a ref. var. that is defined as the superclass of B (A). And once you've stored it in a superclass ref. var., you can use instanceof to figure out which subclass the ref.var. object actually is.

Example:


Did I get it?


Thanx,
DMad
[ March 08, 2005: Message edited by: David Madouros ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the righthand side is java.lang.Object you'll always get true.
After all, everything derives from java.lang.Object!
Of course if you're comparing Objects across classloaders this is no longer the case but that's a somewhat obscure scenario that many people will never encounter.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the righthand side is java.lang.Object you'll always get true.


Not so.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[DMad]: Did I get it?

Pretty much. Your code won't compile, due to an unrelated problem (accessing nonstatic fields from a static method), but your summary of what instanceof is used for seems to be accurate.
 
David Madouros
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

accessing nonstatic fields from a static method



Doh! Back to the books for more study. Thanks for all the help guys.


DMad
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic