Java char 以什么编码存储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7019504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In what encoding is a Java char stored in?
提问by pepsi
Is the Java char type guaranteed to be stored in any particular encoding?
Java char 类型是否保证以任何特定编码存储?
Edit: I phrased this question incorrectly. What I meant to ask is are char literals guaranteed to use any particular encoding?
编辑:我错误地表述了这个问题。我想问的是char 文字是否保证使用任何特定的编码?
回答by Ryan Stewart
"Stored" where? All Strings in Java are represented in UTF-16. When written to a file, sent across a network, or whatever else, it's sent using whatever character encoding you specify.
“储存”在哪里?Java 中的所有字符串都以 UTF-16 表示。当写入文件、通过网络发送或其他任何方式时,它会使用您指定的任何字符编码发送。
Edit:Specifically for the char
type, see the Character docs. Specifically: "The char data type ... are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities." Therefore, casting char
to int
will always give you a UTF-16 value ifthe char
actually contains a character from that charset. If you just poked some random value into the char
, it obviously won't necessarily be a valid UTF-16 character, and likewise if you read the character in using a bad encoding. The docs go on to discuss how the supplementary UTF-16 characters can only be represented by an int
, since char
doesn't have enough space to hold them, and if you're operating at this level, it might be important to get familiar with those semantics.
编辑:专门针对该char
类型,请参阅Character docs。具体来说:“char 数据类型......基于原始 Unicode 规范,该规范将字符定义为固定宽度的 16 位实体。” 因此,铸造char
到int
永远给你一个UTF-16的值,如果在char
实际包含从字符集的字符。如果您只是在 中插入一些随机值char
,它显然不一定是有效的 UTF-16 字符,同样,如果您使用错误的编码读取字符。文档继续讨论补充 UTF-16 字符如何只能由 表示int
,因为char
没有足够的空间来容纳它们,如果您在此级别操作,熟悉这些语义可能很重要。
回答by Stephen C
A Java char
is conventionally used to hold a Unicode code unit; i.e. a 16 bit unit that is part of a valid UTF-16 sequence. However, there is nothing to prevent an application from putting any 16 bit unsigned value into a char
, irrespective of what it actually means.
Javachar
通常用于保存Unicode 代码单元;即作为有效 UTF-16 序列一部分的 16 位单元。但是,没有什么可以阻止应用程序将任何 16 位无符号值放入 a 中char
,而不管它的实际含义如何。
So you could say that a Unicode code unit can berepresented by a char
and a char
canrepresent a Unicode code unit ... but neither of these is necessarilytrue, in the general case.
所以你可以说一个 Unicode 代码单元可以用 a 表示char
,achar
可以表示一个 Unicode 代码单元……但在一般情况下,这些都不一定正确。
Your question about how a Java char
is stored cannot be answered. Simply said, it depends on what you mean by "stored":
您char
无法回答有关如何存储Java 的问题。简单地说,这取决于你所说的“存储”是什么意思:
If you mean "represented in an executing program", then the answer is JVM implementation specific. (The
char
data type is typically represented as a 16 bit machine integer, though it may or may not be machine word aligned, depending on the specific context.)If you mean "stored in a file" or something like that, then the answer is entirely dependenton how the application chooses to store it.
如果您的意思是“在执行程序中表示”,那么答案是特定于 JVM 实现的。(
char
数据类型通常表示为 16 位机器整数,尽管它可能会或可能不会机器字对齐,具体取决于特定上下文。)如果您的意思是“存储在文件中”或类似的东西,那么答案完全取决于应用程序选择如何存储它。
Is the Java char type guaranteed to be stored in any particular encoding?
Java char 类型是否保证以任何特定编码存储?
In the light of what I said above the answer is "No". In an executing application, it is up to the application to decide what a char
means / contains. When a char
is stored to a file, the application decides how it wants to store it and what on-disk representation it will use.
根据我上面所说的,答案是“不”。在一个正在执行的应用程序中,由应用程序决定一个char
意味着/包含什么。当 achar
存储到文件中时,应用程序决定如何存储它以及它将使用什么磁盘表示。
FOLLOWUP
跟进
What about char literals? For example, 'c' must have some value that is defined by the language.
字符文字呢?例如,'c' 必须有一些由语言定义的值。
It depends on the character literal form, and what the character is. For instance, 'c' will have the value of the bottom 16 bits of the Unicode codepoint for lowercase 'c'. But a literal expressed as '\uxxxx' may no represent a valid Unicode codepoint. Or (depending on that the application means) it may not represent a character at all.
这取决于字符文字形式,以及字符是什么。例如,'c' 将具有小写 'c' 的 Unicode 代码点的后 16 位的值。但是表示为 '\uxxxx' 的文字可能不代表有效的 Unicode 代码点。或者(取决于应用程序的意思)它可能根本不代表一个字符。
This is also (potentially) complicated by the encoding of the source code file. It is theoretically possible to represent your source code in a custom character encoding in which (for the sake of argument) uppercase letters are encoded as lowercase, and vice versa. If you did this, and you were able to register the corresponding Charset encoder and decoder before launching the compiler, then a literal that looks like 'c'
(viewing the input as ASCII or UTF-8) would actually have the value 67
in the compiler program rather than 99
.
这也(可能)因源代码文件的编码而变得复杂。理论上可以用自定义字符编码来表示您的源代码,其中(为了论证)大写字母被编码为小写,反之亦然。如果这样做,并且能够在启动编译器之前注册相应的 Charset 编码器和解码器,那么看起来像'c'
(将输入视为 ASCII 或 UTF-8)的文字实际上将67
在编译器程序中具有该值,而不是99
.
回答by Ernest Friedman-Hill
Originally, Java used UCS-2 internally; now it uses UTF-16. The two are virtually identical, except for D800 - DFFF, which are used in UTF-16 as part of the extended representation for larger characters.
最初,Java 在内部使用 UCS-2;现在它使用UTF-16。两者几乎相同,除了 D800 - DFFF,它们在 UTF-16 中用作较大字符的扩展表示的一部分。