Java: Syntax and Meaning Behind "[B@1Ef9157"? Binary/Address

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

You're looking at the object ID, not a dump of the contents.

  • The [ means array.
  • The B means byte.
  • The @ separates the type from the ID.
  • The hex digits are an object ID or hashcode.

If the intent is to print the contents of the array, there are many ways. For example:

byte[] in = new byte[] { 1, 2, 3, -1, -2, -3 };
System.out.println(byteArrayToString(in));

String byteArrayToString(byte[] in) {
char out[] = new char[in.length * 2];
for (int i = 0; i < in.length; i++) {
out[i * 2] = "0123456789ABCDEF".charAt((in[i] >> 4) & 15);
out[i * 2 + 1] = "0123456789ABCDEF".charAt(in[i] & 15);
}
return new String(out);
}

A complete list of the type nomenclature can be found in the JNI documentation.

Here is the entire list:

  • B - byte
  • C - char
  • D - double
  • F - float
  • I - int
  • J - long
  • L***fully-qualified-class*;** - between an L and a ; is the full class name, using / as the delimiter between packages (for example, Ljava/lang/String;)
  • S - short
  • Z - boolean
  • [ - one [ for every dimension of the array
  • (***argument types*)***return-type* - method signature, such as (I)V, with the additional pseudo-type of V for void method

What does the class class [B represents in Java?

Those are arrays of primitives ([B == byte[], [C == char, [I == int). [Lx; is an array of class type x.

For a full list:

[Z = boolean
[B = byte
[S = short
[I = int
[J = long
[F = float
[D = double
[C = char
[L = any non-primitives(Object)

Also see the Javadoc for Class.getName.

Java references values are addresses values?

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.



Related Topics



Leave a reply



Submit