
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 --------- }
- Related Articles
- Differences between Compact Strings and Compressed Strings in Java 9?
- IntBuffer compact() method in Java
- FloatBuffer compact() method in Java
- DoubleBuffer compact() method in Java
- ShortBuffer compact() method in Java
- ByteBuffer compact() method in Java
- CharBuffer compact() method in Java
- What are automatic modules in Java 9?
- What are the enhancements in Internationalization in Java 9?
- What are the different module types in Java 9?
- What are the advantages of Modules in Java 9?
- What are the improvements in Process API in Java 9?
- What are the useful commands in JShell in Java 9?
- What are the changes in Memory Management in Java 9?
- What are the CompletableFuture API improvements in Java 9?\n

Advertisements