基本类型
Java的四类八种基本数据类型
自动装箱
执行Integer i=2将int型值赋值给Integer类型,会进行自动装箱:
执行该赋值语句时会调用Integer类中valueOf方法进行自动装箱:
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
在Integer类中有一个IntegerCache内部类–自动缓存:
//缓存,创建了一个Integer数组,存放-128 到high(可配置)的值;
//high取值为max(h=127,配置值)
private static class IntegerCache {
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) {
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);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
Integer、Short、Byte、Character、Long这几个包装类的valueOf方法的实现是类似的;
自动拆箱
Integer i1 = 17;
int i2 = 17;
int i3 = 137;
Integer i4 = 137;
System.out.println(i1 == i2); //true
10 System.out.println(i3 == i4); //true
Integer类型和int类型进行==操作时,会进行自动拆箱:
调用Integer类中的intValue方法,比较的是value,不是Integer实现的地址;
public int intValue() {
return value;
}
Integer i1 = 17;
Integer i2 = 17;
Integer i3 = 137;
Integer i4 = 137;
// ==
System.out.println(i1 == i2);
System.out.println(i3 == i4);
// equals
System.out.println(i1.equals(i2));
15 System.out.println(i3.equals(i4));
==比较的仍是integer实例;
Integer类重写了equals方法:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
Integer、Short、Byte、Character、Long这几个包装类的intValue方法的实现是类似的;
Integer类型进行算术运算时,会自动拆箱;
总结
Integer a = 1;
Integer b = 2;
Integer c = 3;
// 虽然“==”比较的是引用的是否是同一对象,但这里有算术运算,如果该引用为包装器类型则会导致自动拆箱
System.out.println(c==(a+b));//true
/*执行过程:
先对c调用intValue方法,得到int 3
再依次对a和b调用intValue方法,得到1和2相加得到int 3;
调用==比较int 3 和int 3;
*/
// equals 比较的是引用的对象的内容(值)是否相等,但这里有算术运算,如果该引用为包装器类型则会导
// 致自动拆箱,再自动装箱
// a+b触发自动拆箱得到值后,再自动装箱与c比较
System.out.println(c.equals(a+b));//true
/*执行过程:
依次对a和b调用intValue方法,得到int 1和int 2;
相加得到int 3;
对 int 3自动封箱:调用Integer中的valueOf方法,得到Integer 3;
调用equals方法比较;
*/
Long g = 3L;
// 首先a+b触发自动拆箱后值为int型,所以比较的是值是否相等(int型和long型比较==隐式类型转换)
System.out.println(g==(a+b));//true
/*执行过程:
调用Long中的longValue方法对g进行拆箱,得到long 3;
对a和b调用Integer中的intValue方法,得到 int 1和int 2,,
相加,得到int 3;
比较long 3和int 3;自动类型转换;
*/
long l=3;
int i=3;
System.out.println(l==i);//true
// 首先a+b触发自动拆箱后值为int型,自动装箱后为Integer型,然后g为Long型
System.out.println(g.equals(a+b));//false
/*执行过程:
对a和b调用intValue,然后相加,得到int 3;
对int 3调用valueOf进行封箱,得到integer 3;
调用Long的equals方法比较(方法如下);
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
*/
Long h = 2L;
// 首先a+h触发自动拆箱后值为long型,因为int型的a会自动转型为long型的,g然后自动装箱后为Long型,而g也为Long型
System.out.println(g.equals(a+h));
/*执行过程
对a调用intValue方法得到int 1;
对h调用longValue方法得到long 2;
相加,得到 long 3;
对long 3调用Long类中的longValue方法进行封箱,得到Long 3;
调用Long中的equals方法比较;
*/