SCJP 5.0<br />Preparing SCWCD
Originally posted by Alejandro Galvan:
Hi,
I tried to replicate your experiment as:
(...)
And I didn't get a "2" in the output, I think you should not base your answers in probability, you should read and understand better how the compiler and the JVM works if you want success, the answers given by the book are correct, remember that, when hashCode is not overriden, it use the method from the super class (in this case from class Object) and that method from Object class return the memory address of the object, so, two different objects will have two different hashcodes, so the equals method won't be called.
Regards,
Alejandro Galvan wrote:you should read and understand better how the compiler and the JVM works if you want success, the answers given by the book are correct, remember that, when hashCode is not overriden, it use the method from the super class (in this case from class Object) and that method from Object class return the memory address of the object, so, two different objects will have two different hashcodes, so the equals method won't be called.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:Relax Gijs . The language of Alejandro might be harsh. But he was surely trying to help. So just relax
Gijs Peek wrote:
Anyway, in the answer to question 11 the book mentions that for the purpose of the exam, the hashCode implementation in object does produce unique hashcodes (as far as I can see, they don't mention this earlier in the chapter).
Gijs Peek wrote: I don't know where you read that two different objects will have two different hashcodes, but this does not seem to be true.
hashCode
public int hashCode()Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:
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.
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.
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.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
Returns:
a hash code value for this object.
See Also:
equals(java.lang.Object), Hashtable
SCJP 5.0<br />Preparing SCWCD
Alejandro Galvan wrote:I don't know if you are aware of the API, if you don't, check it, it is a good place to know the entire Java Application Program Interface, go to http://java.sun.com/javase/6/docs/api/, there, navigate to class java.lang.Object and you'll find the method hashCode and its explanaiton:
hashCode
public int hashCode()Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:
(...)As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
(...)
As I have several years of java experience, I am aware of the API, and I still don't see any promises for hashcode uniqueness there, only a JVM implementation guideline for attempting to make them as distinct as reasonably practical.
Alejandro Galvan wrote:Also, I'm posting here the put method from the HashMap class (you can download the source code from the Sun Microsystems site)
I already read that, which is how I knew that the actual JVM implementation checks the hashcode before invoking the equals() method. The point I would like to stress is that since this behavior is not guaranteed, no assumptions can be made about this. This is why the answer is false.
I do understand, however, which answer is expected from me on the exam, and this is probably not the place to complain about invalid exam questions. So only one point remains: the answer to ch. 6 q. 4 states that distinct hashcodes will be put in distinct buckets. This should be modified - 2 distinct hashcodes could be mapped onto a single bucket - the chance is approximately 1 in <hashtable capacity>.
Alejandro Galvan wrote:As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |