Search code examples
javanumberformatexception

What is the difference between Bytes.toString(bytes) and bytes.toString()?


I have an integer that is represented by array of bytes.

byte[] result = getResult();
resultInt1 = Integer.parseInt(Bytes.toString(result));//1               
resultInt2 = Integer.parseInt(result.toString());//2

In first way everything works fine, but in the second I catch NumberFormatException.

What is the difference between these two methods?


Solution

  • Arrays do not override toString().

    Therefore, bytes.toString() does not return anything meaningful; instead, it will return something like [B@18c28a. ([B is the internal representation of an array of byte)