The difference between char and byte

Source: Internet
Author: User

Very many people have just started to learn (including me, has been learning Java for more than a year) Ken will be the char and byte of the two data types have doubts, confusion, today specially checked a lot of data, the byte and char two types are summarized and comparative, 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 ), size range is 0-65535 ;Char is a a binary Unicode character,JAVA with Char To represent a character .

The following examples are used to compare the differences between the two:

1, char is unsigned, can represent an integer, cannot represent a negative number, and byte is a signed, capable of representing -128-127;

char C = (char)-3; Char does not recognize negative numbers and must be cast otherwise, even after casting, System.out.println (c) is not recognized; byte D1 = 1;byte D2 = -1;byte D3 = 127; The hypothesis is byte d3 = 128; Error byte D4 =-128; The hypothesis is byte d4 =-129; error System.out.println (D1); System.out.println (D2); SYSTEM.OUT.PRINTLN (D3); System.out.println (D4);

The result is:


2. Char can be a table literal character,byte cannot, as in:

Char e1 = ' Medium ', e2 = ' country '; byte f= (Byte) ';//must be cast otherwise error System.out.println (E1); SYSTEM.OUT.PRINTLN (E2); System.out.println (f);

The result is:


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

byte g = ' B ';//b the corresponding ASCII is 98char h = (char) G;char i = 85;//u corresponding ASCII is 85int j = ' h ';//h corresponding ASCII is 104system.out.println (g); System.out.println (h); System.out.println (i); System.out.println (j);
The result is:

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: 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.