• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

instanceof Operator

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I didn't quite understand this statement :

"Even if the object being tested is not an actual instantiation of the class type on the right side of the operator, instanceof will still return true if the object being compared is assignment compatible with the type on the right."

given in the SCJP book by Kathy Sierra and Bert Bates.



In the above code I didn't quite understand the output of obj2. Please do explain and clear my concept.

Thanks
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As i understand, the statement means that 'if the object [i.e. obj2] is not at instance of the parent class, but that object [obj2] is type of parent class then instanceof will return true".

-dk
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddharth Bhargava wrote:In the above code I didn't quite understand the output of obj2. Please do explain and clear my concept.


obj2 is referring to an object of "Child" at runtime. But "Child" extends "Parent" so it inherits everything from the "Parent", hence instanceof returns true (this is what assignment compatible means in the below statement).

"Even if the object being tested is not an actual instantiation of the class type on the right side of the operator, instanceof will still return true if the object being compared is assignment compatible with the type on the right."



 
Marshal
Posts: 76864
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind about "child" and "parent" (which are not the official Java terms anyway): look in the Java Language Specification and follow the link to ยง5.2 and see exactly what instanceof does. You will have to follow lots of links to find the whole story, and it is by no means easy to read.

You know that passing null to instanceof always results in false, don't you?
 
it's a teeny, tiny, wafer thin ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic