This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about ==" Watch "question about ==" New topic
Author

question about ==

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
When using == to compare reference variables, the compiler will verify that the classes are the same or in the same inheritance hierarchy, otherwise it'll throw a compiler error

can you guys provide example(s) of this statement? specially when the compiler might throw a compilation error...


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
Andres, is exactly the same as with the instanceof operator or casting. If in the above example you try:
if (d instanceof D2) {} // compiler error!
or:
d = (D2) d2; // compiler error!
They must be in the same inheritence hierarchy.
Miki


Miki<br /> <br />SCJP 1.4, SCBCD 1.3
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: question about ==