The difference between char and byte

Source: Internet
Author: User

A lot of beginners (including me, have been learning Java for more than a year) Ken will be on the char and byte two data types have doubts, confusion, today specially looked at a lot of data, the byte and char two types are summarized and compared, first to share the results with you:

Byte is a byte data type, is a signed type, occupies 1 bytes, and has a size range of-128-127. Char is a character data type, is unsigned, occupies 2 bytes (Unicode code), the size range is 0-65535; char is a 16-bit binary Unicode character, and Java uses Char to represent a character.

Here are some examples to compare the differences:

1. Char is unsigned, can represent an integer, cannot represent a negative number, and byte is a signed type, which can represent 128-127;

[HTML]View Plaincopy
  1. char C = (char)-3;//char does not recognize negative numbers, must be cast otherwise error, even after casting, does not recognize
  2. System.out.println (c);
  3. byte d1 = 1;
  4. byte d2 =-1;
  5. byte d3 = 127;//If it is byte d3 = 128; error
  6. byte d4 =-128;//If it is byte d4 =-129; Error
  7. System.out.println (D1);
  8. System.out.println (D2);
  9. SYSTEM.OUT.PRINTLN (D3);
  10. System.out.println (D4);

The result is:

?
1
-1
127
-128

2, char can be a table Chinese characters, byte can not, such as:

[HTML]View Plaincopy
    1. Char e1 = ' Medium ', e2 = ' country ';
    2. byte f= (Byte) ' in ';//must be cast otherwise error
    3. SYSTEM.OUT.PRINTLN (E1);
    4. SYSTEM.OUT.PRINTLN (E2);
    5. System.out.println (f);

The result is:

In
Country
45

3, char, byte, int for English characters, can be converted to each other, such as:

[HTML]View Plaincopy
    1. byte g = ' B '; b corresponds to ASCII is 98
    2. Char h = (char) g;
    3. char i = 85; U corresponds to ASCII is
    4. int j = ' H '; H corresponds to ASCII is 104
    5. System.out.println (g);
    6. System.out.println (h);
    7. System.out.println (i);
    8. System.out.println (j);

The result is:
98
B
U
104

Welcome to the "Java Dream Team" learning Group: 226159645

The difference between char and byte

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.