Differences between char and byte

Source: Internet
Author: User

Many beginners may be confused about the two data types char and byte. Today, I have checked a lot of information and summarized and compared the two data types byte and char, first, share the results with you: byte is a byte data type, which is signed and occupies 1 byte. The size range is-128-127. Char is a character data type, unsigned, occupies 2 bytes (Unicode Code); the size range is 0-65535; char is a 16-bit binary Unicode character, JAVA uses char to represent a character. The following example compares the differences between the two: 1. Char is unsigned and can represent an integer rather than a negative number. byte is signed, it can represent the number of-128-127; for example, [html] char c = (char)-3; // char cannot recognize negative numbers and must be forcibly converted; otherwise, an error is returned, system cannot be recognized even after forced conversion. out. println (c); byte d1 = 1; byte d2 =-1; byte d3 = 127; // if it is byte d3 = 128; the system reports an error of byte d4 =-128; // if it is byte d4 =-129, an error is returned. out. println (d1); System. out. println (d2); System. out. println (d3); System. out. println (d4); Result :? 1-1127-1282. char can contain Chinese characters, but byte cannot. For example, [html] char e1 = '中', e2 = 'guo'; byte f = (byte) '中'; // must be forcibly converted; otherwise, System is reported. out. println (e1); System. out. println (e2); System. out. println (f); the result is: Chinese 453, char, byte, int for English characters, can be converted to each other, such as: [html] www.2cto. combyte g = 'B'; // the ASCII value of B is 98 char h = (char) g; char I = 85; // U corresponds to ASCII 85 int j = 'H'; // h corresponds to ASCII 104 System. out. println (g); System. out. println (h); System. out. println (I); System. out. println (j); Result: 98bU104

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.