开发者

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

Is there any reason why Java char primitive data type is 2 b开发者_运维百科ytes unlike C which is 1 byte?

Thanks


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.


char in Java is UTF-16 encoded, which requires a minimum of 16-bits of storage for each character.


In Java, a character is encoded in UTF-16 which uses 2 bytes, while a normal C string is more or less just a bunch of bytes. When C was designed, using ASCII (which only covers the english language character set) was deemed sufficient, while the Java designers already accounted for internationalization. If you want to use Unicode with C strings, the UTF-8 encoding is the preferred way as it has ASCII as a subset and does not use the 0 byte (unlike UTF-16), which is used as a end-of-string marker in C. Such an end-of-string marker is not necessary in Java as a string is a complex type here, with an explicit length.


In previous languages like C ASCII notations are used. And the range is 127 , for 127 unique symbols and language characters.

While JAVA comes with a feature called "INTERNATIONALIZATION", that is all the Human Readable characters(Including Regional symbols) are also added into it , and the range is also increased , so more the memory required , the system to unify all these symbols is "Standard Unicode System", and so that this Unification requires that additional byte in JAVA.

The first byte remains as it is and ASCII characters are ranged to 127 as in C,C++ but unified characters are than appended to them.

So 16-bits for char in JAVA and 8-bits for char in C.


Java™ Tutorials:

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).


Java uses UNICODE (Universal Code) representation which accepts all the language formats in the world.

     ASCII  American Standard Code for Information Exchange

     ISO 8859-1 for western European Countries

     KOI-8 for Russian

     GB10830 & BIG-5 for Chinese
         

In this 1 byte is reserved for ASCII & remaining 1 byte can accept any other language => 2byte for char

while C/C++ uses only ASCII Representation => 1 byte for char


Java used as a internationalize so, its work in different languages and need to space more than one byte, that's why its take 2byte of space in char. for eg the chinese language can't hanfle one byte of char.


As we know c suppors ASCII where as java supports Unicode which contains 3 things that is 1-ASCII 2-extended ASCII 3-local language character ASCII is a subset of unicode.ASCII supports only English language where as Unicode supports multinationals language.otherwise java character is encoded within UTF-16 which uses 2 byte.for all of the reason and as the Unicode is the extended version of ASCII ,so it uses 16 bit insted of 8 bit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜