File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how does == work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how does == work" Watch "how does == work" New topic
Author

how does == work

Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
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.



Suman Mummaneni
Bangalore
India
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
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

Joined: Jan 01, 2007
Posts: 333
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
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Lots of people forget how equals() works, as you see from the Angelika Langer article I quoted earlier.
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
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


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
He said you might get equals() returning true and == returning false.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Actually, thinking about it, maybe I have been mistaken, and have read Gavin's post wrongly.

Sorry.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Dec 14, 2004
Posts: 87
Hi Ernest,
Thank you for clarifying my confusion.
Thanks to all for responding to the post
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Yes, Ernest and Gavin, I apologise; I got it wrong.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thanks Campbell,
It at least made me look at equals again, and you added soem interesting references to the topic
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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.....


Creativity is nothing but Breaking Rules
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Suman Mummaneni
Ranch Hand

Joined: Dec 14, 2004
Posts: 87
Hi Rob and Santosh,
Thanks for clarifying things. Now I really a much better idea of equals(), hashCode() and ==.
Keith Godwin
Greenhorn

Joined: Oct 27, 2008
Posts: 4
.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how does == work