Java基本数据类型
8种原始类型 | 容量 | 对应包装类型 |
---|---|---|
byte(字节) | 8 位 | Byte |
shot(短整型) | 16位 | Short |
int(整型) | 32 位 | Integer |
long(长整型) | 64 位 | Long |
float(浮点型) | 32 位 | Float |
double(双精度) | 64 位 | Double |
char(字符型) | 16 位 | Character |
boolean(布尔型) | 1 位 | Boolean |
需要注意点:
- 基本数据类型可以直接使用“==”进行判等。
- 封装类型需要使用
equals()
进行判等,如果是同“==”只是判断实例的引用值 装箱与拆箱:
装箱是使用Integer.valueOf()。如果Integer或Long装箱的时候会使用到缓存-128~127。不在缓存范围内才会new出一个对象。
注意,只是装箱的时候才会使用到缓存,直接new的时候不会使用缓存。
Integer a = 3; Integer b = 3; Integer c = Integer.valueOf(3); System.out.print(a == b); //true System.out.print(a == c); //true Integer m = new Integer(3); Integer n = new Integer(3); System.out.print(n == m); //false
- 封装类型与基本数据类型使用“==”会自动拆箱
各数据类型按容量大小由小到大排列为:
byte ——> short, char ——> int ——> long ——> float ——> double
基本类型之间的转换原则:
- 运算