Number of char bytes in Java

Source: Internet
Author: User

I used to think that char occupies a byte. Later I found it far from that simple. The number of bytes of char in Java, which is related to encoding. The UTF-8 is used. The English character occupies 1 byte, and the Chinese character occupies 3 bytes. The following is the test result in Ubuntu:

Public static void main (string [] ARGs) throws ioexception {string chnstr = ""; system. out. println ("length of two Chinese Character:" + chnstr. getbytes ("UTF-8 "). length); string Engstr = "en"; system. out. println ("length of two English character:" + Engstr. getbytes ("UTF-8 "). length );}

Output:

length of two Chinese character: 6length of two English character: 2

On the Internet, an interview question is circulating: What is the number of bytes of a Chinese char in Java? The answer is uncertain (2, 3, 4), which is related to encoding. The following program proves this answer:

Public static void main (string [] ARGs) throws ioexception {string chnstr = "Hua"; system. out. println ("length of one Chinese Character in GBK:" + chnstr. getbytes ("GBK "). length); system. out. println ("length of one Chinese Character in UTF-8:" + chnstr. getbytes ("UTF-8 "). length); system. out. println ("length of one Chinese Character in UNICODE:" + chnstr. getbytes ("Unicode "). length );}

Output

length of one Chinese character in gbk: 2length of one Chinese character in UTF-8: 3length of one Chinese character in Unicode: 4

In the official Oracle documents, Unicode characters in Java are interpreted as follows:

Unicode character representations

The Char data type (and therefore the value that a character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities.
The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. the range of legal code points is now u + 0000 to U + 10 FFFF, known as Unicode scalar value. (refer to the definition of the U + N notation in
Unicode standard .)
The set of characters from u + 0000 to U + FFFF is sometimes referred to as the Basic multilingual plane (BMP). characters whose code points are greater than u + FFFF are called supplementary
Characters. the Java platform uses the UTF-16 representation in char arrays and in the string and stringbuffer classes. in this representation, supplementary characters are represented as a pair of char values, the first from the high-Surrogates range, (\ ud800-\ udbff ),
The second from the low-Surrogates range (\ udc00-\ udfff ).
A char value, therefore, represents basic multilingual plane (BMP) code points, including the surrogate code points, or code units of the UTF-16 encoding. An int value represents
All Unicode code points, including supplementary code points. the lower (least significant) 21 bits of int are used to represent Unicode code points and the upper (most significant) 11 bits must be zero. unless otherwise specified, the behavior with respect
To supplementary characters and surrogate char values is as follows:

  • The methods that only accept a char value cannot support supplementary characters. they treat char values from the surrogate ranges as undefined characters. for example, character. isletter ('\ ud840') returns false, even though this specific value if followed
    By any low-surrogate value in a string wowould represent a letter.
  • The methods that accept an int value support all Unicode characters, including supplementary characters. for example, character. isletter (0x2f81a) returns true because the code point value represents a letter (a cjk ideograph ).

In the Java se api documentation, Unicode code point is used for character values in the range between U + 0000 and U + 10 FFFF, and Unicode code
Unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode glossary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.