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:
?
1
-1
127
-128
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:
In
Country
45
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:
98
B
U
104
The difference between char and byte