Home > Java > javaTutorial > Why Does My Java Byte Array Print as \'[B@1ef9157\' Instead of Its Contents?

Why Does My Java Byte Array Print as \'[B@1ef9157\' Instead of Its Contents?

Susan Sarandon
Release: 2024-10-30 05:03:02
Original
417 people have browsed it

Why Does My Java Byte Array Print as

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:

  • [: Indicates that it is an array
  • B: Specifies that it is a byte array
  • @: Separates the type from the object ID
  • 1ef9157: A hexadecimal representation of the object ID

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>
Copy after login

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:

  • B - byte
  • [ - array
  • @ - separator between type and ID
  • 1ef9157 - object ID

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template