jubincn

导航

Java中char的字节数

以前一直以为char占一个字节,后来发现远没这么简单。Java中char的字节数,和编码有关。使用UTF-8,英文字符占1个字节,中文占3个字节。下面在是在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 );
	}

输出:

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

在网上流传这样一个面试题:Java中一个中文char的字节数是?答案为不确定(2,3,4),跟编码有关。下面这段程序可以证明这个答案:

	public static void main(String[] args) throws IOException {		
		String chnStr = "华";
		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 );
	}
输出

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


在Oracle的官方文档中,关于Java中Unicode字符表示是这样解释的:

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+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the 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 would 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+10FFFF, 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.


posted on 2013-04-21 20:19  jubincn  阅读(1949)  评论(0编辑  收藏  举报