简介
在Java中一共有8种基本数据类型
,其中有4种整型、2种浮点类型、boolean类型和char类型。
- 整型
基本类型 | 字节 | 位数 | 取值范围 |
---|
int | 4字节 | 32bit | -2^31 ~ 2^31 - 1 |
short | 2字节 | 16bit | -32768 ~ 32767 |
long | 8字节 | 64bit | -2^63 ~ 2^63 - 1 |
byte | 1字节 | 8bit | -128 ~ 127 |
- 浮点类型
基本类型 | 字节 | 位数 | 取值范围 |
---|
float | 4字节 | 32bit | 1bit(符号位) + 8bit(指数位) + 23bits(尾数位) |
double | 8字节 | 64bit | 1bit(符号位) + 11bit(指数位) + 52bits(尾数位) |
- boolean
基本类型 | 字节 | 位数 | 取值范围 |
---|
boolean | 1字节 | 8bit | true,false |
- char
基本类型 | 字节 | 位数 | 取值范围 |
---|
char | 2字节 | 16bit | xx |
验证
@Test
public void test2(){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
System.out.println(Float.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
}
2147483647
-2147483648
32767
-32768
9223372036854775807
-9223372036854775808
127
-128
3.4028235E38
1.4E-45
1.7976931348623157E308
4.9E-324
其他
@Test
public void test3() {
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);
System.out.println(Integer.MIN_VALUE - 1);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MAX_VALUE == Integer.MIN_VALUE - 1);
}
-2147483648
-2147483648
true
2147483647
2147483647
true
思考
Java中能够定义的数组的最大长度是多少?