A byte cannot accommodate unicodes of characters from various languages of the world. So using a byte array we cannot have a string of different languages. Why these classes use byte array instead of character array ?
UPDATE:
class First
{
public static void main(String[] args)
{
System.out.println();
String s = "\u0935\u0902\u0926\u0947 \u092e\u093e\u0924\u0930\u092e\u094d";
String s1 = "वंदे मातरम्";
System.out.println(sb);
System.out.println(sb1);
}
}
I think above Strings take two bytes for each character. How they can be accommodated in one byte ?
The use of byte[]
is an optimization introduced in Java 9. The goals/motivation of this change is described in JEP 254: Compact Strings.
Summary
Adopt a more space-efficient internal representation for strings.
Goals
Improve the space efficiency of the String class and related classes while maintaining performance in most scenarios and preserving full compatibility for all related Java and native interfaces.
Non-Goals
It is not a goal to use alternate encodings such as UTF-8 in the internal representation of strings. A subsequent JEP may explore that approach.
Motivation
The current implementation of the String class stores characters in a char array, using two bytes (sixteen bits) for each character. Data gathered from many different applications indicates that strings are a major component of heap usage and, moreover, that most String objects contain only Latin-1 characters. Such characters require only one byte of storage, hence half of the space in the internal char arrays of such String objects is going unused.
Description
We propose to change the internal representation of the String class from a UTF-16 char array to a byte array plus an encoding-flag field. The new String class will store characters encoded either as ISO-8859-1/Latin-1 (one byte per character), or as UTF-16 (two bytes per character), based upon the contents of the string. The encoding flag will indicate which encoding is used.
String-related classes such as AbstractStringBuilder, StringBuilder, and StringBuffer will be updated to use the same representation, as will the HotSpot VM's intrinsic string operations.
This is purely an implementation change, with no changes to existing public interfaces. There are no plans to add any new public APIs or other interfaces.
The prototyping work done to date confirms the expected reduction in memory footprint, substantial reductions of GC activity, and minor performance regressions in some corner cases.