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

What is compact string in Java 9?

PHPz
Release: 2023-09-11 17:09:02
forward
811 people have browsed it

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template