The bytes occupied by char in java: 1. The char in the internal code in java is encoded in UTF16, and one char occupies two bytes; 2. The char in the foreign code in java is encoded in UTF8. One character occupies [1~6] bytes.
#Before discussing this issue, we need to distinguish between unicode and UTF.
unicode: A unified character number, which only provides mapping between characters and numbers. The number of symbols is constantly increasing and has exceeded one million. Details: [https://zh.wikipedia.org/zh-cn/Unicode]
UTF: unicode transformation format. Defines the encoding method of numbers in unicode. UTF8 and UTF16 are two of the implementation methods. Among them, utf8 is a variable-length representation, and the length may be 1 to 6 bytes; utf16 is a variable-length representation, and the length may be 2 or 4 bytes. Details: UTF8 [https://zh.wikipedia.org/zh-cn/UTF-8] UTF16 [https://zh.wikipedia.org/zh-cn/UTF-16]
Next, we need to distinguish between internal encoding and external encoding.
Inner code: The encoding method of char and string in memory when a certain language is running.
Outer code: Except for the inner code, all are outer codes.
It should be noted that the encoding method in the object code file (executable file or class file) generated by source code compilation belongs to foreign code.
Let’s take a look at the internal code first
The internal code in JVM uses UTF16. In the early days, UTF16 was encoded using a fixed-length 2-byte encoding. Two bytes can represent 65536 symbols (in fact, it can actually represent less than this), which was enough to represent all characters in Unicode at that time. However, with the increase of characters in Unicode, 2 bytes cannot represent all characters. UTF16 uses 2 bytes or 4 bytes to complete the encoding. To deal with this situation, Java uses a pair of char to represent characters that require 4 bytes, taking into account forward compatibility requirements. Therefore, char in Java takes up two bytes, but some characters require two chars to represent them.
Foreign code
Java's class file uses UTF8 to store characters, that is to say, the characters in the class occupy 1 to 6 bytes.
During Java serialization, characters are also encoded in UTF8, accounting for 1 to 6 characters.
Summary:
The char in the internal code (running memory) of Java is encoded using UTF16. One char occupies two bytes, but some characters require Represented by two chars. So, one character will occupy 2 or 4 bytes.
char in java Chinese and foreign code is encoded using UTF8, and one character occupies 1 to 6 bytes.
In UTF16 encoding, English characters occupy two bytes; most Chinese characters (especially commonly used Chinese characters) occupy two bytes, and individual Chinese characters (unicode-encoded Chinese characters will be added later) , usually rare words that are rarely used) occupy four bytes.
In UTF8 encoding, English characters occupy one byte; most Chinese characters occupy three bytes, and some Chinese characters occupy four bytes.
EOF
Related free learning recommendations: java basic tutorial
The above is the detailed content of How many bytes does char occupy in java?. For more information, please follow other related articles on the PHP Chinese website!