Experience and talent are independent of age
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Experience and talent are independent of age
Experience and talent are independent of age
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Himanshu Kansal wrote:
According to the hashCode contract if equals gives true, then hashCode must give same int result for both objects. But it is not necessary that it will give so. I would have marked D as an answer. Any ideas on this?
[SCJP 6.0]
Given two objects a and b of the same class, and if equals() and hashCode() are implemented correctly for that class, which of the following must hold true?
The options were:
A. If a.equals(b) then a.hashCode() == b.hashCode()
B. If !a.equals(b) then a.hashCode() == b.hashCode()
C. If a.equals(b) then a.hashCode() != b.hashCode()
D. If !a.equals(b) then a.hashCode() != b.hashCode()
Anastasia Sirotenko wrote: It is nessessary that if two objects are equal they have the same hashCode();
Nitish Bangera wrote:1 more thing, a and b are not objects, they are references.
Nitish Bangera wrote: The only true thing is A.
Experience and talent are independent of age
Himanshu Kansal wrote:Does this mean that equals should return true ONLY when references point to the SAME object? If yes, then this does not happen actually.
Himanshu Kansal wrote:
My point is that the compiler won't refuse to compile if things do not go by the contract. To state an option that must be true we need to have a definitive logic; not some policy.
Henry Wong wrote:No. You can prove this simply by creating two different strings with the same value -- and see that the equals() method does indeed returns true.
Experience and talent are independent of age
My point is that the compiler won't refuse to compile if things do not go by the contract. To state an option that must be true we need to have a definitive logic; not some policy.
Experience and talent are independent of age
Himanshu Kansal wrote:Does "implemented correctly" necessarily mean that it follows the policy? Shouldn't it go with respect to the compiler.
I perfectly understand that to answer the question, we need to refer the contract. But that is only to get the correct theoretical answer. In my opinion it should be enforced somehow and not just left for the developers to practice by choice.
[SCJP 6.0]