为什么Java char原语会占用2个字节的内存? [英] Why does the Java char primitive take up 2 bytes of memory?
问题描述
Java char原始数据类型是2个字节有什么理由不像C那样是1个字节吗?
Is there any reason why Java char primitive data type is 2 bytes unlike C which is 1 byte?
谢谢
推荐答案
最初设计Java时,预计任何Unicode字符都适合2个字节(16位),因此 char
和字符
是相应设计的。实际上,Unicode字符现在最多可以需要4个字节。因此,内部Java编码UTF-16要求补充字符使用2个代码单元。基本多语言平面中的字符(最常见的)仍然使用1.每个代码单元使用Java char
。这篇 Sun文章解释得很清楚。
When Java was originally designed, it was anticipated that any Unicode character would fit in 2 bytes (16 bits), so char
and Character
were designed accordingly. In fact, a Unicode character can now require up to 4 bytes. Thus, UTF-16, the internal Java encoding, requires supplementary characters use 2 code units. Characters in the Basic Multilingual Plane (the most common ones) still use 1. A Java char
is used for each code unit. This Sun article explains it well.
这篇关于为什么Java char原语会占用2个字节的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!