This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Given this code, which of the following statements are true?
1) The hashCode method is correctly implemented 2) The hashCode method is not correct because it is inconsistant with the equals method 3) The code will not compile because there is a circular reference to the hashCode method 4) The code will not compile because the return value of this.hashCode is not an int value 5) The code will not compile because it does not implement the comparable interface
what is the output since the mock exam didnot give the answer,i am asking you guys.Until now,i have attended mock exam questions in hashcode like "if the equals() method returns false,the hashcode() comparision == might return true".
In order to find the answer of the preceding one,i executed that program like
Those errors at the program only i got.
Also,can anyone clear the meaning of 2 & 3 options for me,please..
When you made your test program, you changed hashCode() in a small but very significant way; this change is what's causing the stack overflow error. It's rather important that you understand why your change has this effect -- knowing what super.hashCode() means is most definitely important for the exam.
The answer to the question is "1". To understand "2", read the Javadoc for the Object.hashCode() and Object.equals() methods closely. "3" is just gibberish; one doesn't speak of "circular references to methods" in Java. You could interpret it to mean a recursive call, but it's not.
Thanks for the reply Ernest.But still i am having trouble on this program. super.hashcode() will execute Object.hashcode().right?Over there(according to the API) for each different objects,different hashcode will generated. But when i executed the above program with the mein method
when i commentout line4 upto line6,and the executiong result is
Not sure what you are asking. Are you saying because you have a class named Object, that StringBuffer should now inherit from your class, instead of the java.lang.Object class in the core libs? Or are you modifying the core libs?
Joined: Jan 05, 2006
Sorry for the cofusion.I haven't noticed that the super class name, i have given is Object.
StringBuffers cannot override equals() method.so Object class equals() method will run. Then what is the use of Object1 overridden equals() method.is there any way to execute line 1 and line 2. Thanks in advance.