1.基本数据类型和引用数据类型
基本数据类型-----引用数据类型(包装类)-------继承关系
byte Byte 直接继承Number间接继承Object
short Short
int Integer
long Long
float Float
double Double
char Character 直接继承Object
boolean Boolean
2.为什么引入包装类
- 操纵类,将基本数据类型封装为对应的包装类,为了获取某些属性和方法
- 集合:只能存放引用数据类型
3.Integer类
- 使用Integer不需要导包
- Integer底层是一个int类型数据,就是对这个数据进行封装
- 代码
Integer a = new Integer(1);
Integer b = 3;
//自动装箱:int----->Integer
Integer m1 = 129;
Integer m2 = 129;
System.out.println(m1==m2);//false
//对于自动装箱来说,在-128~127中,直接返回给你数
//如果在-128~127之外,要开辟空间
int m3 = m1.intValue();//将Integer转换为int
int m4 = Integer.parseInt("123");//将String转换为int
Integer的缓存机制: Integer是对小数据(-128-127)是有缓存的,再jvm初始化的时候,数据-128-127之间的数字便被缓存到了本地内存中,如果初始化-128~127之间的数字,会直接从内存中取出,不需要新建一个对象.。可以通过设置虚拟机参数来修改最大的缓存范围,因为源码里并没有直接给最大缓存值赋值
static final int low = -128;//最小缓存值
static final int high;// 最大缓存值
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;//给最大缓存值赋值