" + x); the output looks like ...">

Java link values ​​are address values?

When I do this:

int x[] = new int[2];
System.out.println("...> " + x);

      

the output looks like this: [I@1b67f74

so the hexadecimal number refers to the memory address where the object was allocated?

and [I

what does it mean?

+2


source to share


4 answers


No, this hexadecimal number should not be interpreted as the memory address in which the object resides. In fact, it is the hash code of the object. The API documentation Object.toString()

says:

The toString method for the Object class returns a string consisting of the name of the class whose object is the instance, the at-sign `@ ', and the unsigned hexadecimal representation of the object's hash code. In other words, this method returns a string equal to the value:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

      

The API documentation java.lang.Object.hashCode()

says:

As far as reasonably practical, the hashCode method defined by the Object class returns different integers for different objects. (This is usually accomplished by converting the internal address of the object to an integer, but this implementation is not required in the JavaTM programming language.)



So, for the Sun JVM, if you don't override the method hashCode()

, then this is indeed the memory address of the object, but there is no guarantee that it is, so you shouldn't depend on it.

There is no (real, reliable) way (that works on any JVM) to get the memory address of an object in pure Java; Java has no pointers, and references are not exactly the same as pointers.

Section 4.3.2 of the Java Virtual Machine Specification explains what this means [I

; in this case, it means that your variable is an array int

.

+10


source


C: Java: Syntax and meaning behind "[B @ 1ef9157"? Binary / address?



The hexadecimal digits are the object identifier or hash code.

+1


source


[I

means an array ( [

) of integers ( I

).

0


source


[I denote the class name of the int array. the address number in the vm, but since hashCode is usually overridden, it is impractical to use it directly to identify the object. for this use System.identityHashcode (Object x) which returns the same value in a reliable way.

0


source







All Articles