Java的基本类型占字节数
数据类型 | 占用字节数 | 位数 | 取值范围 |
Byte | 1 | 8 | -2的7次方到2的7次方-1 |
Short | 2 | 16 | -2的15次方到2的15次方-1 |
Int | 4 | 32 | -2的31次方到2的31次方-1 |
char | 2 | 16 | |
Float | 4 | 32 | 3.402823e+38 ~ 1.401298e-45 |
Long | 8 | 64 | -2的63次方到2的63次方-1 |
Double | 8 | 64 | 1.797693e+308~ 4.9000000e-324 |
boolean | 1(前7位是0,即1/8bit) | 8 | 0/1 |
注:Float是单精度,Double是双精度。所以,0.1d==0.1f是false。
1 System.out.println("byte==>min-max:"+Byte.MIN_VALUE +"~"+Byte.MAX_VALUE +",位数:"+Byte.SIZE); 2 System.out.println("int==>min-max:"+Integer.MIN_VALUE +"~"+Integer.MAX_VALUE +",位数:"+Integer.SIZE); 3 System.out.println("short==>min-max:"+Short.MIN_VALUE +"~"+Short.MAX_VALUE +",位数:"+Short.SIZE); 4 System.out.println("float==>min-max:"+Float.MIN_VALUE +"~"+Float.MAX_VALUE +",位数:"+Float.SIZE); 5 System.out.println("double==>min-max:"+Double.MIN_VALUE +"~"+Double.MAX_VALUE +",位数:"+Double.SIZE); 6 System.out.println("long==>min-max:"+Long.MIN_VALUE +"~"+Long.MAX_VALUE +",位数:"+Long.SIZE);
byte==>min-max:-128~127,位数:8
int==>min-max:-2147483648~2147483647,位数:32
short==>min-max:-32768~32767,位数:16
float==>min-max:1.4E-45~3.4028235E38,位数:32
double==>min-max:4.9E-324~1.7976931348623157E308,位数:64
long==>min-max:-9223372036854775808~9223372036854775807,位数:64