为什么 Java char 原语占用 2 个字节的内存? [英] Why does the Java char primitive take up 2 bytes of memory?

查看:31
本文介绍了为什么 Java char 原语占用 2 个字节的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 位),因此 charCharacter 是相应设计的.事实上,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆