[Solved] What is the encoding of Java byte type?


Bytes don’t have an encoding. They’re bytes.

See the Javadoc of String.getBytes():

Encodes this String into a sequence of bytes using the platform’s default charset

So, it’s whatever your default charset is. You can find out what that is at runtime using Charset.defaultCharset().

If you want the bytes in a particular charset, specify it, e.g.:

str.getBytes(StandardCharsets.UTF_8)
// or
str.getBytes(Charset.forName("the name of the desired charset"))

(And note that System.out.println(str.getBytes()); won’t print anything related to the contents of the array).

4

solved What is the encoding of Java byte type?