Background
?? Char contains a few bytes it may be remembered that in school when the book is written in 2 bytes, has not been the drill, today we will explore the end of a char how many bytes?
Char
?? Char is used to store characters at the beginning of the design, but there are so many characters in the world, if there are 1 bytes, it means that only 256 is stored, which is obviously inappropriate, and if you have two bytes, you can store 65536 kinds. This number matches the number of language characters in most countries. The Java team then defaults to Unicode as the encoding, and a char is stored as 2 bytes.
?? There's two problems here?
???? 1. is Java char bound to have two bytes?
???? 2. Can Chinese characters be stored in char?
?? Answer the first question first. Char must be two bytes? No, this is related to the character encoding we choose, and if the "Iso-8859-1" code is used, then a char will only have one byte. If the "UTF-8" or "GB2312", "GBK" and other encoding format? These encoding formats use dynamic lengths, and if they are English characters, everyone is a byte. In Chinese, "UTF-8" is three bytes, while "GBK" and "GB2312" are two bytes. For Unicode, however, it is two bytes in any case.
?? Then answer the second question, for a char if "iso-8859-1" to store, it certainly cannot store a Chinese, and for "UTF-8", "GB2312", "GBK", most Chinese characters can be stored.
Summarize
?? The length of the char and whether it can store the Chinese characters is related to the encoding format. When we encode the cross-platform encoding, we should set the corresponding format when encoding, decoding, to prevent the error caused by the codec.
Test point: The sword is an offer----the first character that appears only once
A char in Java contains several bytes