gpt4 book ai didi

java - Java 对 String 的内部表示是什么?修改过的UTF-8? UTF-16?

转载 作者:IT老高 更新时间:2023-10-28 20:36:18 26 4
gpt4 key购买 nike

我在 Java 的内部表示中搜索了 String,但我得到了两种看起来可靠但不一致的 Material 。

一个是:

http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451

它说:

Java uses UTF-16 for the internal text representation and supports a non-standard modification of UTF-8 for string serialization.

另一个是:

http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8

它说:

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.

修改过的 UTF-8?还是 UTF-16?哪一个是正确的? Java在内存中使用一个char需要多少字节?

请告诉我哪个是正确的以及它使用了多少字节。

最佳答案

Java uses UTF-16 for the internal text representation

String 和 StringBuilder 等在 Java 中的表示是 UTF-16

https://docs.oracle.com/javase/8/docs/technotes/guides/intl/overview.html

How is text represented in the Java platform?

The Java programming language is based on the Unicode character set, and several libraries implement the Unicode standard. The primitive data type char in the Java programming language is an unsigned 16-bit integer that can represent a Unicode code point in the range U+0000 to U+FFFF, or the code units of UTF-16. The various types and classes in the Java platform that represent character sequences - char[], implementations of java.lang.CharSequence (such as the String class), and implementations of java.text.CharacterIterator - are UTF-16 sequences.

在 JVM 级别,如果您使用 -XX:+UseCompressedStrings(这是 Java 6 的某些更新的默认设置)实际的内存表示可以是 8 位,ISO-8859 -1 但仅适用于不需要 UTF-16 编码的字符串。

http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

and supports a non-standard modification of UTF-8 for string serialization.

序列化字符串默认使用 UTF-8。

And how many bytes does Java use for a char in memory?

char 总是两个字节,如果你忽略了在 Object 中填充的需要。

注意:一个代码点(允许字符 > 65535)可以使用一个或两个字符,即 2 或 4 个字节。

关于java - Java 对 String 的内部表示是什么?修改过的UTF-8? UTF-16?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9699071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com