What are Compact Strings in Java 9?


Since Java 9, the JVM optimizes strings by using a new feature called Compact Strings. Instead of having a char[array, a string can be represented as a byte[] array. We can use either UTF-16 or Latin-1 to produce either one or two bytes per character. If JVM detects the string contains only ISO-8859-1/Latin-1 characters, then string uses one byte per character internally.

The string can be represented with a compact string or not is detected when the string is created. This feature has enabled by default and switches off using the -XX:-CompactStrings. It doesn't revert to a char[] implementation and stores all strings as UTF-16.

// In Java 8
public class String {
   private final char[] value; // Stores characters in the string
      ---------
}

// In Java 9
public class String {
   private final byte[] value; // Stores characters in the string
   private final byte coder; // a flag whether to use 1 byte per character or 2 bytes per characters for this string

      ---------
}

Updated on: 19-Mar-2020

232 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements