Home  >  Article  >  Java  >  What does \"[B@1ef9157\" mean when printing byte arrays in Java?

What does \"[B@1ef9157\" mean when printing byte arrays in Java?

Barbara Streisand
Barbara StreisandOriginal
2024-10-28 22:05:02895browse

What does

Deciphering the Meaning Behind "[B@1ef9157" in Java

When working with Java, developers may encounter peculiar prefixes such as "[B@1ef9157" upon attempting to print byte arrays. While these strings may appear cryptic, they hold significant information about the array's structure and contents.

Composition of the Prefix

The prefix "[B@1ef9157" can be broken down into the following components:

  • [ indicates that the object is an array.
  • B signifies that the array contains bytes.
  • @ separates the type ("B") from the identity hashcode.
  • 1ef9157 represents the identity hashcode or object ID.

Interpreting the Prefix

The prefix alone does not reveal the contents of the byte array. It merely signifies that the object is a byte array and provides its unique identifier. The actual values stored in the array are not displayed in this format.

Printing the Array Contents

To view the actual contents of the byte array, various methods are available. One technique involves converting the byte values to hexadecimal characters:

<code class="java">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);
}</code>

Additional Information

For a comprehensive list of type nomenclatures used in Java, refer to the JNI documentation. The following table summarizes the common types:

Type Representation
Byte B
Character C
Double D
Float F
Integer I
Long J
Class L*fully-qualified-class*;;
Short S
Boolean Z
Array [
Method Signature (
Type Representation
Byte B
Character C
Double D
Float F
Integer I
Long J
Class L*fully-qualified-class*;;
Short S
Boolean Z
Array [
Method Signature (argument types;)return-type
argument types;)return-type

The above is the detailed content of What does \"[B@1ef9157\" mean when printing byte arrays in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn