Why am I getting different results while using System.out.println on a serialized int?

-1

I have the following serialization class, which serializes a custom integer variable into 4 bytes byte arrays:

public class Serializer {

    public static byte[] timestampToBytes(int epochSeconds) {

        byte[] result = new byte[4];

        result[0] = (byte) ((epochSeconds & 0xFF000000) >> 24);
        result[1] = (byte) ((epochSeconds & 0x00FF0000) >> 16);
        result[2] = (byte) ((epochSeconds & 0x0000FF00) >> 8);
        result[3] = (byte) ((epochSeconds & 0x000000FF) >> 0);

        return result;        

    }

    public static int timestampFromBytes(byte[] epochSeconds) {

        int result = 0;

        result = (int) ((epochSeconds[0] << 24) & 0xFF000000);
        result |= (int) ((epochSeconds[1] << 16) & 0x00FF0000);
        result |= (int) ((epochSeconds[2] << 8) & 0x0000FF00);
        result |= (int) ((epochSeconds[3] << 0) & 0x000000FF);

        return result;

    }    

}

The class is consumed in the following test as follows:

public class Main {

  public static void main(String[] args) throws InterruptedException  {

    byte[] timestampBytes = new byte[4];
    timestampBytes = Serializer.timestampToBytes(1572037187);
    System.out.println(Serializer.timestampToBytes(1572037187));
    System.out.println(timestampBytes);
    System.out.println(Serializer.timestampFromBytes(Serializer.timestampToBytes(1572037187)));
    System.out.println(Serializer.timestampFromBytes(timestampBytes));

  }

}

Now I get the following results in the output console:

[B@2f4d3709
[B@4e50df2e
1572037187
1572037187

While serialization methods seem to work properly, I do not understand why when I directly print the byte[] elements using timestampBytes and Serializer.timestampToBytes(1572037187) different results are provided. The point is deserialization seem to work fine in both cases.

java
arrays
serialization
asked on Stack Overflow Nov 15, 2019 by M.E. • edited Nov 15, 2019 by M.E.

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0