File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about ==" Watch "question about ==" New topic

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!
d = (D2) d2; // compiler error!
They must be in the same inheritence hierarchy.

Miki<br /> <br />SCJP 1.4, SCBCD 1.3
I agree. Here's the link:
subject: question about ==
It's not a secret anymore!