How many bytes of English and Chinese characters take in java?

瘦欲@ 提交于 2021-01-02 07:59:09

问题


import java.io.UnsupportedEncodingException;

public class TestChar {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String cnStr = "龙";
        String enStr = "a";
        byte[] cnBytes = cnStr.getBytes("UTF-8");
        byte[] enBytes = enStr.getBytes("UTF-8");

        System.out.println("bytes size of Chinese:" + cnBytes.length);
        System.out.println("bytes size of English:" + enBytes.length);

        //  in java, char takes two bytes, the question is: 
        char cnc = '龙'; // will '龙‘ take two or three bytes ?
        char enc = 'a'; // will 'a' take one or two bytes ?
    }
}

Output :

   bytes size of Chinese:3

   bytes size of English:1

Here, My JVM is set as UTF-8, from the output, we know Chinese character '龙' takes 3 bytes, and English character 'a' takes one byte. My question is:

In Java, char takes two bytes, here, char cnc = '龙'; char enc = 'a'; will cnc only takes two bytes instead of 3 bytes ? And 'a' takes two bytes instead of one byte ?


回答1:


The codepoint value of is 40857. That fits inside the two bytes of a char.

It takes 3 bytes to encode in UTF-8 because not all 2-byte sequences are valid in UTF-8.




回答2:


UTF-8 is a variable-length character encoding, where characters take up 1 to 4 bytes.

A Java char is 16 bits. See 3.1 Unicode in the Java Language Specification to understand how exactly Java handles Unicode.




回答3:


Internally, Strings/chars are UTF-16, so it'll be the same for both: Each char will be 16bits.

byte[] cnBytes = cnStr.getBytes("UTF-8");

UTF-8 is a variable length encoding, so the Chinese char takes more bits because it's out of the ASCII character range.



来源:https://stackoverflow.com/questions/59039660/how-many-bytes-of-english-and-chinese-characters-take-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!