Home  >  Article  >  Java  >  How Does Java Store Strings Internally: Modified UTF-8 or UTF-16?

How Does Java Store Strings Internally: Modified UTF-8 or UTF-16?

Barbara Streisand
Barbara StreisandOriginal
2024-11-11 20:25:02435browse

How Does Java Store Strings Internally: Modified UTF-8 or UTF-16?

Java's Internal String Representation: Modified UTF-8 or UTF-16?

When exploring the internal representation of strings in Java, conflicting information arises. Some sources claim Java uses Modified UTF-8, while others assert it employs UTF-16. To clarify this discrepancy, let's delve into the details.

According to Oracle's Java documentation, the Java platform heavily leans on the Unicode character set. Consequently, the primitive char data type signifies an unsigned 16-bit integer. This integer can represent a Unicode code point within a particular range or the code units of UTF-16. Crucially, character sequences like strings, character arrays, and implementations of the CharSequence interface are all UTF-16 sequences in Java.

However, it's essential to note that JVM implementations can utilize compressed strings. When -XX: UseCompressedStrings (a default setting in certain Java 6 updates) is enabled, strings that don't necessitate UTF-16 encoding may be stored in an 8-bit format, namely, ISO-8859-1.

Regarding string serialization in Java, UTF-8 is the default encoding. Therefore, when strings are serialized, they utilize UTF-8.

Finally, Java represents a char using two bytes, allowing for code points up to 65,535. Code points beyond this limit occupy two characters, resulting in the consumption of four bytes.

The above is the detailed content of How Does Java Store Strings Internally: Modified UTF-8 or UTF-16?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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