• 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

how does == work

 
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a small question if the hash code of the objects are equal will == also give the same result.

Object a = new Object();
Object b = new Object();

a.getHashCode()==b.getHashCode() returns true

then will

a == b also be true.


 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should check out the sun java docs on equals(), hashCode() and == or just check out the API dos for Object.

First, even if equals returns true, == may return true or false, as == compares the object refences in memory.
equals() will return true if the objects have equals value. equals() is definded by the class.

hashCode is a little tricky, if i remember, it is possible for two different objects to return the same hashCode(). OF course this has implications for hashtables and the like.

Hope this helps.
Gavin
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suman Mummaneni wrote:
Object a = new Object();
Object b = new Object();

a.getHashCode()==b.getHashCode()



By the way,there is no such a method called getHashCode() in Object class. it is a hashCode()

Hope this helps
 
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gavin Tranter wrote: . . . First, even if equals returns true, == may return true or false, as == compares the object refences in memory.
equals() will return true if the objects have equals value. equals() is definded by the class. . . .

You have described an equals method which has been incorrectly overridden.
Apart from the equals() method specification, you should look for the chapter in Effective Java by Bloch and Angelika Langer's equals() article.

By the way, Suman Mummaneni, you ought to test that code snippet you posted. See whether you get true or false from either of your "==" propositions.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought i might have defined it incorrectly.
It seems I often forget:


.......returns true if and only if x and y refer to the same object (x == y has the value true).



Thank you for beinging it to my attention
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lots of people forget how equals() works, as you see from the Angelika Langer article I quoted earlier.
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Thanks for the post, plese look at the below code snippet, when you run this the hasCode() will be the same. But the == will fail so what does this eqactly evalutate. I am assuming that == will compare the memory location of the two objects and equals() method will use be compared the object data.


I am assuming that hashCode should be returning the memory loacation of the object is it correct ? Or does hashCode() returns some unique ID



 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suman Mummaneni wrote:
I am assuming that hashCode should be returning the memory loacation of the object is it correct ? Or does hashCode() returns some unique ID



You're assuming wrong. hashCode() is a non-final method overridden by many classes, so you can say virtually nothing about the values that it returns. hashCode() is supposed to return values which are equal for any two object for which equals() is true, and as widely distributed as possible, so that for two unequal objects it's likely that their hashCode()s are unequal. But that's about it.

There is no relationship at all between hashCode() and the "==" operator, other than the trivial one that if == evaluates to true for two references, then both references wlll return the same hashCode(), since they are both referring to the same object.

Campbell, I don't understand what you're saying is wrong with Gavin's statement about equals(); sounds perfect to me. Can you elaborate?
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He said you might get equals() returning true and == returning false.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, thinking about it, maybe I have been mistaken, and have read Gavin's post wrongly.

Sorry.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:He said you might get equals() returning true and == returning false.



And indeed, you may. new String("foo").equals("foo") is true; new String("foo") == "foo" is false.
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest,
Thank you for clarifying my confusion.
Thanks to all for responding to the post
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Ernest and Gavin, I apologise; I got it wrong.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell,
It at least made me look at equals again, and you added soem interesting references to the topic
 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Equals and hash code are the two public methods defined in Object class these two methods are overridden by String and wrapper class if you want your class to put it in collection then you should override both hash code and equals method in object class. by default equals method in object class returns true if both references of that class refer to same instance means both hash codes are equal. and by default equals method does not depend on the members of the class it simply checks both references equal.but when you want equals method to behave on the members of the class you must override the equals method and hash code method.if two equals method returns true then both references must return same hash code.if both hash code returns same integer then equals method may return true or false.....
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

santhosh.R gowda wrote:Equals and hash code are the two public methods defined in Object class. These two methods are overridden by String and wrapper class. If you want your class to put it in collection then you should override both hash code and equals method in object class. By default equals method in object class returns true if both references of that class refer to same instance, means both hash codes are equal. And by default equals method does not depend on the members of the class; it simply checks both references equal. But when you want equals method to behave on the members of the class you must override the equals method and hash code method. If two equals method returns true then both references must return same hash code. Iif both hash code returns same integer then equals method may return true or false.....


Some line breaks, or even periods would be nice. I made an attempt, and it already reads a lot better.
 
Suman Mummaneni
Ranch Hand
Posts: 99
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob and Santosh,
Thanks for clarifying things. Now I really a much better idea of equals(), hashCode() and ==.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic