Hello,

I am trying to understand what a byte array is.

public class ByteArrayExample
{
    public static void main (String args[])
    {
        byte[] array = "1".getBytes();
        System.out.println (array);
    }
}

The output of this program is : [B@44770739

Is this a hexadecimal representation? If any of the members can explain how "1" is represented as "44770739" in a byte array, I would be very grateful.

Thank you!

That's the default output created by the toString method that every object and array inherits from Object. It means

[    this is an array
B    of bytes
44770739   and that is its unique hash value

(very precise, and totally useless!)

To convert an array to a useful printable string use the toString method from the Arrays class, eg
System.out.println (Arrays.toString(array));

Edited 3 Years Ago by JamesCherrill

James,

That gives me - [B@3df78040

Is this the same as:

0011 1101 1111 0111 1000 0000 0010 0000

I suppose what I am lost at is how does this represent "1"?

Thank you!

   byte[] array = "1".getBytes();
   System.out.println (Arrays.toString(array));

Will give the output

   [49]

ie an array of one element containing the decimal value 49 - which is the UniCode (and ASCII) code for the character '1'

Okay, first of all that apparently meaningless output is NOT representing "1" in any language, not even Klingon :)

As James mentioned, the part after [B@ is an unique ID for the array so every time you recompile your code its gonna print out something different.

Now,

"1".getBytes() returns a byte array which as per your call contains '1' - the number. (Byte in java is just another integer datatype with the shortest range, -128 to 127. And a byte array doesn't mean it will give you the binary representations of the numbers it contains)

So 2 places where you went wrong:
1. You were trying to print the array itself and not its contents.
2. System.out.println (Arrays.toString(array)); as James mentioned will give you 49 which is the ASCII representation of numeric 1. (Did you try it correctly? It shoudn't have given you that [B@... stuff again! Did you add import java.util.* at the top?)

Finally, to get a clearer picture and to understand that byte array is nothing but just another int array where you can only put integers < 127 and > -128, try the following code:

public class ByteArrayExample
{
    public static void main (String args[])
    {
        byte[] array = {1, 2, 3};
        for (int i = 0; i < array.length; i++) {
            System.out.println (array[i]);
        }
    }
}

Edited 3 Years Ago by NP-complete

trust me, that does not return "[B@3df78040".
I don't know how you added that, and since JamesCherill showed you exactly how to change your code, I also have no idea how you could 've gotten it wrong, but apparently, you did.
re-read his post and try again.

Hello All,

Thanks a ton! It was my error all the way through. It's very clear now.

Regards

This question has already been answered. Start a new discussion instead.