Understanding "[B@1ef9157": Binary or Address?
When working with byte arrays in Java, you may encounter the "[B@ prefix" when printing the byte array. This can be confusing, as you might expect to see the actual contents of the array rather than a seemingly random string of characters.
Explanation
The "[B@ prefix" is not a representation of the byte array's contents. It is the object ID of the array. This ID identifies the array in memory.
Structure of the Object ID
The object ID consists of:
Printing Byte Array Contents
If your goal is to print the actual values of the byte array, you will need to use a method that specifically converts the bytes to a printable format. For example, you can use the byteArrayToString() method:
<code class="java">byte[] in = new byte[] { 1, 2, 3, -1, -2, -3 }; System.out.println(byteArrayToString(in));</code>
The byteArrayToString() method converts the byte array to a string of hexadecimal characters, making it easier to read.
JNI Type Nomenclature
The "[B@ prefix" is part of the JNI (Java Native Interface) type nomenclature. This nomenclature is used to identify Java types in native code:
For more information on JNI type nomenclature, refer to the official JNI documentation.
The above is the detailed content of Why Does My Java Byte Array Print as \"[B@1ef9157\" Instead of Its Contents?. For more information, please follow other related articles on the PHP Chinese website!