I am confused with byte and char in Java. I have the following program segment :
byte b = 65;
char c = 65;
short s = 65;
System.out.println("b:"+b+" c:"+c+ " s:"+s); // outputs b:65 c:A s:65
The bit representation of b, c and s are same they all are :: 00000000 01000001 . My question is if the bits are same then how they act differently - b as byte, c as char and s as short ? Another question is : char c = 65;
why this is a correct statement ? it does not give error though I am assigning int value to a char.
Thanks.
how they act differently ?
Refer the JLS :
The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units.
The values of the integral types are integers in the following ranges:
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
For long, from -9223372036854775808 to 9223372036854775807, inclusive
For char, from '\ ' to '\' inclusive, that is, from 0 to 65535
Another difference will be their Wrapper Classes differ : byte
to Byte
, char
to Character
and short
to Short
.
char c = 65;
why this is a correct statement ?
char , whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).
byte
is boxed to Byte
char
is boxed to Character
short
is boxed to Short
Those 3 classes have 3 different toString()
method. That's why they have different displays.
Then the conversions from int
to byte
, char
, short
are done automatically for you, then it is boxed to their corresponding boxing class.
1) their bit representation is not the same, it's 01000001
for byte and 0000000001000001
for short and char.
2) the difference is that byte and short are signed integers and char is a 16-bit Unicode character
3) 65 is not int, it's a constant, if you try to assign an int you will get an error
int x = 65;
char c = x; <-- error
They do not behave differently as far data reprezentation is considered, the difference is only that they can hold values in different intervals.
As for char every character has it's integers representation that is why it is a valid syntax in java.
All primitive types ( boolean, char, short, int, ...
) are actually bit arrays in memory.
The type of the variable only defines in what range this variable can take value:
boolean
1 bit [range 0-1] char
16 bits [range 0 to 216-1 or \ to \] byte
8 bits [range -128 to 127] short
16 bits [-32768 to 32767] int
32 bits [-2147483648 to 2147483647] char
is represented by bits, or complementary hex, dec, oct nubmer. It does not matter. That's why you can assign a number to it and this nubmer is matched to Unicode representation of that number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.