Skip to main content

(How) do I use single byte characters in Java

Short answer is use Java 9 or later.

Prior to Java 9 each character in a String uses 2 bytes.  With Java 9, String class figures out whether to use 1 byte or 2 bytes depending on the encoding required - UTF 8 or UTF 16.  

If any character requires 2 bytes for encoding, all characters in the String are allocated 2 bytes.  Otherwise 1 byte is allocated for each char. 

This is the default behavior from Java 9.

Comments