Addressing the "[B@" Enigma: Understanding Java Byte Array Notation
The peculiar "[B@" representation encountered when printing byte arrays in Java has often puzzled developers. What does it signify, and how can we decipher its meaning?
Decoding the Symbolism
The notation "[B@" is not a hexadecimal representation of byte array contents but rather an object descriptor. Each component represents a specific aspect:
Printing Array Contents Effectively
To display the actual contents of a byte array, rather than the object ID, you can employ various methods:
Explicit Iteration and Conversion:
<code class="java">byte[] in = {1, 2, 3, -1, -2, -3}; for (byte b : in) { System.out.print(String.valueOf(b) + " "); }</code>
Hexadecimal String Conversion:
<code class="java">System.out.println(Base64.getEncoder().encodeToString(in));</code>
Custom String Conversion:
<code class="java">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); }</code>
Understanding JNI Nomenclature
The "[B@" notation is part of a larger system for describing types in JNI (Java Native Interface). Here's a complete list:
Comprehending this notation enables you to navigate the complex world of Java data representation with confidence.
The above is the detailed content of What is the \'[B\\@\' Enigma: Understanding Java Byte Array Notation?. For more information, please follow other related articles on the PHP Chinese website!