A char in Java contains several bytes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.