• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals method in Object class

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
Thanks,
Brian
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind!!!
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 ...
Brian
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 ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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()?
Corey
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Brian
[ February 22, 2002: Message edited by: Brian Lugo ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Brian
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone confirm this
Object A = new Long(7);
Long L = new Long(7);
If(A.equals(L)) System.out.println("Long Objects Equal");
else
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

Thanks
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vinny75,
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.
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic