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 equals method in Object class 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 "equals method in Object class" Watch "equals method in Object class" New topic

equals method in Object class

Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Hi All!
I am a bit confused about the equals method in the java.lang.Object class.
Here is what the API says:
"The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true)."
Okay, here is a code taken from Exam Cram and modified a little bit:

The output is:
Long Objects Equal
Long Objects 2nd Test Not Equal
String Objects Equal
String Objects 2nd Test Not Equal
My Question:
Why this anomaly?
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
I got it. equals method of Long is called because Long is derived from Object in the first case. This applies to the second case also.
Sometimes you just gotta think harder ...
Sorry for the post ...
PS - I am not sure if the explanation provided in the Exam Cram is confusing or not:
"The Long Object created in line "x" (3) does not lose its identity when cast to Object A, so the equals method knows the class is correct and compares the values".
[ February 22, 2002: Message edited by: Brian Lugo ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Brian Lugo:

Why this anomaly?

What's strange about that? The code does exactly what I would expect. The '==' operator, when used on object references, only compares the references to determine if they reference the same object. Look here:

The references contained in o and q are the same (because of the assignment operator). That means that they both reference the same object and the '==' operator will return true. p, however, references a different object. Therefore, comparing that reference with o or q, we get a result of false, indicating that the two references do not reference the same object.
Now, when dealing with the equals() method, the Object class provides an implementation that does the same thing as the '==' operator. However, some class override this method to check for a different type of equality. The String class and the wrapper classes do just that. They each define their own type of equality. For Strings, they are considered equal if they contain the same list of characters (case sensitive). For the wrapper classes, they check the internal primitive values to determine if they are equal.
Does that help clear up the difference between '==' and equals()?

SCJP Tipline, etc.
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Hi Corey!
I guess you did'nt read my 2nd post before you posted your post . I got the concept couple of minutes after I posted the first post.
I guess my confusion stemmed from the explanation provided in Exam Cram - Chapter 3 Question 10.
Thanks anyways,
[ February 22, 2002: Message edited by: Brian Lugo ]
Jennifer Wallace
Ranch Hand

Joined: Nov 30, 2001
Posts: 102
Very interesting Question Brian!

IT took me a while to figure out why the above code prints "Equal" when it would invoke Object's equals() method because the reference(B) is an object.
But Of course not! Though the reference(B) is Object, the equals() method in String would be invoked because the real object held by the Object reference(B) is a String and String has a equals() method which overrides the method equals() in Object. So equals() of String would be invoked and according to String Equality rules, contents are equal and hence "Equal" is printed.
[ February 22, 2002: Message edited by: Jennifer Wallace ]
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Thanks Jennifer!
I think in this case the ambigious (to me) explanation provided by the writer(s) of Exam Cram prompted me to look into this matter a bit deeper. And hence, now I will never forget this . I am glad I looked into this or else I would'nt have researched this matter in depth.
I know that this concept is very simple, but lot of times we take things for granted and forget it.
Vinny Srin

Joined: Feb 25, 2002
Posts: 6
Can someone confirm this
Object A = new Long(7);
Long L = new Long(7);
If(A.equals(L)) System.out.println("Long Objects Equal");
System.out.println("Long Objects Not Equal");

Please give me more explanation on this. Does EQUALS return true because the objects being compared are the same long type? Even though they are two different instances of the same object?
By doing so, it overrides what "==" would have normally done by returning False since they are two separate objects? Likeswise with string compare

Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch, a friendly place for java greenhorns
We hope you'll enjoy your stay.
Here is the source code of method equals in class Long:

As you can see the parameter must be an instance of class Long otherwise the method returns false. Then their value must be the same in order for equals to return true...
Moreover, we'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with the rules. Thank you for your cooperation.

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: equals method in Object class
jQuery in Action, 3rd edition