I wrote this code:
byte[] test = {-51};
byte[] test2 = {-51};
byte[] test3 = {-51};
System.out.println(String.valueOf(test));
System.out.println(String.valueOf(test2));
System.out.println(String.valueOf(test3));
And i’ve got a different results:
[B@9304b1
[B@190d11
[B@a90653
Why?
String.valueOf does not have a byte[] argument, so it will be processed as
Objectand thetoString()method will be called, since arrays doesn’t implements this method, Object.toString() will be process in the array and it’s result varies from each instance.If you want to convert
byte[]to String use the constructor String(byte[]) or String(byte[] bytes, Charset charset)Result:
If you want to view the content of the array use the Arrays.toString(byte[])
Result: