什么是byte
byte
,即字节,由8位
的二进制组成。在Java中,byte
类型的数据是8位带符号的二进制数。
在计算机中,8位带符号二进制数的取值范围是[-128, 127],所以在Java中,byte类型的取值范围也是[-128, 127]
。
public static void main(String[] args) throws IOException {
byte a = 6;
System.out.println(a);//6
//byte底层是8位二进制
//该二进制数的取值范围是[-128, 127]
byte b = (byte) 180;
System.out.println(b);//-76
}
byte short int long
byte: -128 to 127.
short: -32,768 to 32,767.
int: -2,147,483,648 to 2,147,483,647.
long: -2^63 to 2^63–1
Byte can hold 8 bit
Short can hold 16 bit
Int can hold 32 bit
Long can hold 64 bit
byte与char的区别
1 byte = 8 bits, signed in 2’s complement
1 char = 16 bits, unsigned, used for unicode characters representation.