This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi, I got it from Master Exam. 11.x=0; 12.if(x1.hashcode()!=x2.hashcode())x=x+1; 13.if(x3.equals(x4)==false)x=x+10; 14.if(x5.equals(x6)==true)x=x+100; 15.if(x7.hashcode()==x8.hashcod())x=x+1000; 16.System.out.println("x=" + x);
if the output is "x=1111", which of the following will always be true?
Answer given: x2.equals(x1)==true I think this answer violates the contract.the correct answer would be x5.hashcode()==x6.hascode(). other than "x2.equals(x1)" the rest are coming under "can be true" so not suitable for the question asked.
If x.equals(y)==true then x.hashCode()==y.hashCode() MUST be true; If x.equals(y)==false then x.hashCode()==u.hashCode() MIGHT be true;
And these rules are applied on Collections that uses Hash Codes for fast searching, because they first search the bucket (hashCode) before search the object (equals), otherwise equals() have nothing to do with hashCode().
They will be equals even with differente hashCodes, but they will not be found in a collection that uses hash codes for fast searching using its own implemented methods. [ December 05, 2008: Message edited by: Fabio Nascimento ]