Java中char的字节数
来源:互联网 发布:厦门市大数据管理中心 编辑:程序博客网 时间:2024/05/12 22:09
以前一直以为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: 6length 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: 2length of one Chinese character in UTF-8: 3length 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).
- Java中char的字节数
- Java中char的字节数
- java中char的字节数
- Java基本类型占用的字节数(char占用几个字节问题)
- Java中boolean占的字节数
- char字节数、char与byte、byte数组合并 Java
- MySQL数据类型char与varchar中数字代表的究竟是字节数还是字符数?
- MySQL数据类型char与varchar中数字代表的究竟是字节数还是字符数?
- 字节与字符及java中char与数据库中char的区别
- Java中char到底是多少字节?
- Java中char占用几个字节
- JAVA中char为两字节存储
- java 中一个char包含几个字节
- Java中char占用几个字节
- Java中char占用几个字节
- 关于Oracle中,char、nchar、varchar、nvarchar存储汉字是所需要的字节数
- 在C/C++中,int、char、double、long所占的字节数
- java 数据类型所占字节数,int char
- Quick Benchmark: Gzip vs Bzip2 vs LZMA vs XZ vs LZ4 vs LZ
- poj1065(木棍加工贪心)
- URL编码问题汇总
- TinyOS LEEP详解
- PQ分区魔术师图解教程
- Java中char的字节数
- 黑马程序员——C#学习日记之字符串处理函数
- VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
- CentOS输入法
- Hello Dojo
- JSON解析-android平台
- 美国人打仗的第二天买黄金!
- django 实现的组织架构树
- 华为离生命企业有多远