Suman Mummaneni
India
Suman Mummaneni wrote:
Object a = new Object();
Object b = new Object();
a.getHashCode()==b.getHashCode()
You have described an equals method which has been incorrectly overridden.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. . . .
.......returns true if and only if x and y refer to the same object (x == y has the value true).
I am assuming that hashCode should be returning the memory loacation of the object is it correct ? Or does hashCode() returns some unique ID
Suman Mummaneni
India
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
Campbell Ritchie wrote:He said you might get equals() returning true and == returning false.
Suman Mummaneni
India
Creativity is nothing but Breaking Rules
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.....
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Suman Mummaneni
India
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |