Strange Wrapper class behavior in java

public class Test {
    public static void main(String args[]) {
        int i = 10;
        Integer a = new Integer(i);
        System.out.println(a);        //tostring method overriden
        System.out.println(a.hashCode());
    }
}

Output :
  10
  10

Now my question is why the method hashCode()is overestimated in this case. If I want to find a reference to an object of an object of a class class a in the above code. How to do it?

+4
source share
3 answers

An object reference to an integer in your case a. Unlike C, in Java you cannot get a reference pointer to an object. hashCodenot used to identify the location address of an object in memory.

From the hashCode API,

Gets the hash code value for the object. This method is supported for hash tables such as HashMap.

, hashCode .

- , System.identityHashCode.

System.identityHashCode(a)
+5

, hashCode

, String, . , (), -: , , ...

-, . ?

System.identityHashCode()

+1

In Java, a hash code helps give a quick hint to a companion between two objects. Since two different ones Integerthat have the same value are equal, they must have the same hash. This is the reason why the value was taken as a hash.

+1
source

All Articles