wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof and equality operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof and equality operators" Watch "instanceof and equality operators" New topic
Author

instanceof and equality operators

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Hi friends,

Can somebody please clear my dought once and for all.



Line 3 generates a compiler error. Ok. But why doesn't line 4 generate one?
The references are of different types... And juding by K&B, they are not from the same class heirechy (which again, should cause a compile-time error).

Please help me. Thanks in advanced.

Cheers, Marzo.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The compile time error you're seeing on line 3 only happens when you are comparing two concrete class references from different inheritance hierarchies. In other words, the compiler knows from the reference type alone that it's impossible for those two references to be referencing the same object because it's impossible for them to be the same class.

Line 4 compiles without a problem because one of the variables is an interface reference (myCol), so the compiler cannot be sure what kind of object that variable references. Here's an example of your program with some more interfaces and classes being compared:



So when you compare a reference variable of an interface type to any other reference variable at all, the compiler will not generate an error.

The reason is that compiler does not know what the actual object type is for a reference variable. It only knows what the reference type is. If either of the reference types is an interface, then it is possible for the two reference variables to be equal. For example, you could make this class:



Note that SubTreeMap IS-A TreeMap and also that SubTreeMap IS-A Collection.

Then in your main method you could put:



Then if you were to compare (myMap==myCol) the result would be true, because the same SubTreeMap object is referenced by both variables.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Thanks for your reply, it has cleared up 99.9 percent of my queries.
The other 0.1 percent is this:



Is the above error caused due to the compiler seeing that bObj1 is of type B and that it can never actually be an A() object?

And where it does compile, is it because the compiler sees that the bObj2 reference is of type A and that it might actually be an A() object at runtime?

Can you just please confirm this for me, and thanks for your reply, it has helped ALOT.

Cheers, Marzo.
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
if(myMap instanceof List) //ok - line 1 System.out.println("OK");
if(myMap instanceof Collection) //ok - line 2 System.out.println("OK");

Why does line1 and line 2 donot give any error..the List and Collection interface are in a different hierarchy when compared to myMap??
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by Marzo Kaahn:
Thanks for your reply, it has cleared up 99.9 percent of my queries.
The other 0.1 percent is this:



Is the above error caused due to the compiler seeing that bObj1 is of type B and that it can never actually be an A() object?

And where it does compile, is it because the compiler sees that the bObj2 reference is of type A and that it might actually be an A() object at runtime?

Can you just please confirm this for me, and thanks for your reply, it has helped ALOT.

Cheers, Marzo.



instanceof takes a single parameter, <type>. <type> is either the name of a Java class, or it is the type descriptor of an array. You are trying to pass an Object here.

If you replace last two lines with:
boolean b1 = (aObj instanceof B);
boolean b2 = (aObj instanceof B);
both of them should work.


Devender Thareja
SCEA, SCBCD, SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof and equality operators