Question

When I do:

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

the output value is like this: [I@1b67f74

so that hex number is concerning to the memory address where the object has been allocated?

and [I what does it meaning?

Was it helpful?

Solution

No, that hex number should not be interpreted as the memory address where the object is located. In fact, it is the hash code of the object. The API documentation of Object.toString() says:

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

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

The API documentation of java.lang.Object.hashCode() says:

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.)

So for Sun's JVM, if you don't override the hashCode() method, then it's indeed the memory address of the object, but there is no guarantee that this is so, 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 does not have pointers, and references are not exactly the same as pointers.

Section 4.3.2 of the Java Virtual Machine Specification explains what the [I means; in this case it means your variable is an array of int.

OTHER TIPS

From: Java: Syntax and meaning behind "[B@1ef9157"? Binary/Address?

The hex digits are an object ID or hashcode.

[I means it's an array ([) of integers (I).

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top