Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashCode and equals" Watch "hashCode and equals" New topic
Author

hashCode and equals

Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 91
Dear all,

In the test CD of K&B book, there is one example:




One of the answers in the CD is



I wonder if this is correct? Because according the the contract between equals and hashCode, "but equals() and hashCode() are bound together by a joint contract that specifies if two objects are considered equal using the equals() method, then they must have identical hashcode values."


In this case, if we have 2 object A and B. Supporse that they have the same code, but A.bal = 2, A.rate = 3, B.bal = 3, B.rate = 2. In this case, equals() return true but these objects do not have the same hashCode, which violate the contract.

Is this answer correct???
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3827

if we have 2 object A and B. Supporse that they have the same code, but A.bal = 2, A.rate = 3, B.bal = 3, B.rate = 2. In this case, equals() return true but these objects do not have the same hashCode, which violate the contract.

Yes, Correct.
Is there anything else mentioned in the question? Have you checked the errata whether anything related to this presents?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1086
    
    1

I think you are correct. Also given equals() method do not satisfy one of the contract point
For any non-null reference value x, x.equals(null) should return false.


Below mentioned is my analysis



Output is

HashCode Contract
1. Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
A.hashCode() 16
A.hashCode() 16
B.hashCode() 24
B.hashCode() 24

2. •If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
A.hashCode() = 16
B.hashCode() = 24
A.equals(B) = true

3. •It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables
A.equals(B) = false
A.hashCode() = 48
B.hashCode() = 24



Equals Contract
1. •It is reflexive: for any non-null reference value x, x.equals(x) should return true.
true
true

2. •It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true
A.equals(B) - true
B.equals(A) - true

3. •It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
A.equals(B) - true
B.equals(C) - true
C.equals(A) - true

4. •It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified
B.equals(A) - true
B.equals(A) - true
B.equals(A) - true

5. •For any non-null reference value x, x.equals(null) should return false.
Exception in thread "main" java.lang.NullPointerException
at aaa.SortOf.equals(SortOf.java:12)
at aaa.SortOf.main(SortOf.java:95)


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Swerrgy Smith
Ranch Hand

Joined: Mar 26, 2010
Posts: 91
Thank all of you for your replies
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
3 It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Does that mean if two objects are unequal (a.equals(b) returns false) then a.hashCode() and b.hashCode() may return same integer results ?

i am confused with the usage of word "not".
Correct me if i am wrong.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3827

sakthi moorthy wrote:3 It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Does that mean if two objects are unequal (a.equals(b) returns false) then a.hashCode() and b.hashCode() may return same integer results ?

Yes, it is not required to return the same hash code unless the objects are equal (by equals() method).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hashCode and equals