A sequence of zero or more characters is known as a string.
In Java programs, a string is represented by an object of the java.lang.String class.
The String class is immutable.
The contents of a String object cannot be modified after it has been created.
The String class has two companion classes, java.lang.StringBuilder and java.lang.StringBuffer.
The companion classes are mutable.
You should use them when the contents of your string can be modified.
Prior to Java 9, the implementation of the String class stored characters in a char array, using two bytes for each character in the string.
Most String objects contain only Latin-1 characters requiring only one byte to store one character in the string.
In most cases, half of the space in the char arrays of such String objects is not used.
Java 9 has changed the internal implementation of the String class to use a byte array to store the contents of the String object.
It stores an encoding flag to indicate if each character in the String takes one byte or two bytes.
This change in Java 9 is an implementation change.
It has efficient use of the memory used by String objects.
No public interface was changed for the String class in Java 9.PreviousNext