0

As far as I know Object.hashCode() method has native implementation. I particularly interested in: is there any randomization in this method? I have class straight forward derived from the Object.class without redefined hashCode method. Correct me if I am wrong but when we call == operator we compare references of such objects, alias compare hashcodes. So do we always get false

//pseudocode
new SomeObject(3) == new SomeObject(3)    

because of randomization involved into Object.hashCode() method?

Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • 4
    "when we call == operator we compare references of such objects" - yes, so hashCode isn't involved at all. It's not clear why you think it would be. – Jon Skeet Oct 09 '15 at 14:53
  • 1
    @Rudik, Don't confuse `equals()` with `==` –  Oct 09 '15 at 14:57
  • http://stackoverflow.com/questions/2237720/what-is-an-objects-hash-code-if-hashcode-is-not-overridden/32454673#32454673 Take a look an answer of @TagirValeev – Sergey Morozov Oct 09 '15 at 15:02
  • @JonSkeet At the page of 613 Bruce Eckel 'Thinking in java 4th edition' is written: `the default implementation of hashCode( ) does use the object address` So was I partially right about randomization as far as address setting process is more or less random? – Rudziankoŭ Oct 10 '15 at 12:21
  • Thanks, @SergeyMorozov, it answered my question – Rudziankoŭ Oct 10 '15 at 12:25

2 Answers2

3

The Object hascode is just a hash, not a pointer. So the == will compare the references not the hashcode.

The general contract of hashCode from spec 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 hash tables.

The keyword native tells the the implementation is done at native code using JNI(Java Native Interface).

Also if you are overriding the hashcode method dont forget that your implementation should satisfy the above contract else you may get wrong output, where it is used. Say HashMap class.

More info: What issues should be considered when overriding equals and hashCode in Java?

Community
  • 1
  • 1
rajuGT
  • 6,224
  • 2
  • 26
  • 44
1

As far as I know Object.hashCode() class is native.

Correct.

I particularly interested in: is there any randomization in this method?

The hashcode returned by Object.hashCose() IS typically based on a machine address of the object at some time during its lifetime. The address, and hence the hashcode may be influenced by a variety of random factors, depending on the application.

However, this does not mean that the hashcodes are random. In fact, if you allocated two objects one after the other and immediately got their hashcodes, it is likely that there will be a strong correlation between the two hashcodes.

Likely ... but not guaranteed.

Correct me if I am wrong but when we call == operator we compare references of such objects.

Correct.

... alias compare hashcodes.

If you are saying that == involves hashcodes, that is INCORRECT. == is implemented by comparing the current machine addresses of the objects.

So do we always get false because of randomization involved into Object.hashCode() method?

No. In your example, you get false because the object references are different!

In fact, it is possible for two distinct Object instances to have the same hashcode.

  o1 == o2 IMPLIES o1.hashcode() == o2.hashcode()    is TRUE

  o1.hashcode() == o2.hashcode() IMPLIES o1 == o2    is FALSE
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216