I searched Java's internal representation for String, but I've got two materials which look reliable but inconsistent.
One is:
http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451
and it says:
Java uses UTF-16 for the internal text representation and supports a non-standard modification of UTF-8 for string serialization.
The other is:
and it says:
Tcl also uses the same modified UTF-8[25] as Java for internal representation of Unicode data, but uses strict CESU-8 for external data.
Modified UTF-8? Or UTF-16? Which one is correct? And how many bytes does Java use for a char in memory?
Please let me know which one is correct and how many bytes it uses.
UTF-16.
From http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp :
The size of a
char
is 2 bytes.Therefore, I would say that Java uses UTF-16 for internal String representation.
java is available in 18 international languages and following UNICODE character set, which contains all the characters which are available in 18 international languages and contains 65536 characters.And java following UTF-16 so the size of char in java is 2 bytes.
Prior to Java 9, the standard in-memory representation used in a Java
String
is UTF-16 code-units held in achar[]
. Modified UTF-8 is used in other contexts; e.g. in ".class" files, and the object serialization format.You can confirm this by looking at the source code of the
java.lang.String
class.With Java 6 update 21 and later, there was a non-standard option (
-XX:UseCompressedStrings
) to enable compressed strings. This feature was removed in Java 7.With Java 9 and later, the behavior if
String
has been changed to use a compact representation for Strings by default. Thejava
command documentation now says this:Note that neither "compressed" or "compact" strings used / use UTF-8 encoding.
See also:
Java stores strings internally as UTF-16 and uses 2 bytes for each character.
The representation for String and StringBuilder etc in Java is UTF-16
https://docs.oracle.com/javase/8/docs/technotes/guides/intl/overview.html
At the JVM level, if you are using
-XX:+UseCompressedStrings
(which is default for some updates of Java 6) The actual in-memory representation can be 8-bit, ISO-8859-1 but only for strings which do not need UTF-16 encoding.http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html
Serialized Strings use UTF-8 by default.
A
char
is always two bytes, if you ignore the need for padding in an Object.Note: a code point (which allows character > 65535) can use one or two characters, i.e. 2 or 4 bytes.