Home >Java >javaTutorial >What is compact string in Java 9?

What is compact string in Java 9?

PHPz
PHPzforward
2023-09-11 17:09:02787browse

Java 9中的紧凑字符串是什么?

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

can indicate that the string will detect whether it contains a compact string when creating the string. This feature is enabled by default and turned off using -XX:-CompactStrings. It does not revert to the char[] implementation and stores all strings as UTF-16.

<strong>// 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

      ---------
}</strong>

The above is the detailed content of What is compact string in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete