Why does the Java char primitive take up 2 bytes of memory?

后端 未结 7 723
有刺的猬
有刺的猬 2020-11-30 03:38

Is there any reason why Java char primitive data type is 2 bytes unlike C which is 1 byte?

Thanks

7条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 04:25

    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.

提交回复
热议问题