原文出自https://www.cnblogs.com/dolphin0520/p/3780005.html,此处我用来复习所以自己又写了一遍
一、什么是装箱,什么是拆箱
自动装箱:
Integer i = 10;
自动拆箱:
Integer i = 10;
int n = i;
从以上代码得知自动装箱就是将基本数据类型自动转化为包装器类型,拆箱就是将包装器类型转化为基本数据类型
自动拆箱、自动装箱在平时生产应用广泛,但是他的底层原理我们也需要了解,以便于我们更好的应用。
下表是基本数据类型对应的包装器类型:
int(4字节) | Integer |
byte(1字节) | Byte |
short(2字节) | Short |
long(8字节) | Long |
double(8字节) | Double |
float(4字节) | Float |
char(2字节) | Char |
boolean(1字节) | Boolean |
运行上面代码并进行反编译得知:
在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。
面试中遇到的拆装箱有关问题
1、下面代码的输出结果是什么?
public class BoxTest {
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i1==i2);
System.out.println(i3==i4);
}
}
答案:
解析:结果表明i1和i2指向的同一个对象,i3和i4指向了不同的对象,这是为什么呢?Integer i1 = 100;这ge过程中发生了自动装箱,由上面反编译的结果可知在这个过程中Integer调用了value.Of(int i)方法,查看源码得知:
public static Integer valueOf(int i) {
// 判断传入的int值是否在-128-127之间,若不是则创建新的对象,若是则直接从缓存池中获取
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
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) {
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;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
200>127所以此时Integer新建了对象,100<127此时的对象是缓存池中就有的
2、下面代码的输出结果是什么?
public class BoxTest {
public static void main(String[] args) {
Double i1 = 100.0;
Double i2 = 100.0;
Double i3 = 200.0;
Double i4 = 200.0;
System.out.println(i1==i2);
System.out.println(i3==i4);
}
}
答案:false;false;
解析: 查看Double.valueOf(double d)源码可知
// 直接创建新对象了
public static Double valueOf(double d) {
return new Double(d);
}
注意: Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
Double、Float的valueOf方法的实现是类似的。
原因:在某个范围内的整型数值的个数是有限的,而浮点数却不是。
3、下面代码的结果是什么?
public class BoxTest {
public static void main(String[] args) {
Boolean i1 = false;
Boolean i2 = false;
Boolean i3 = true;
Boolean i4 = true;
System.out.println(i1==i2);
System.out.println(i3==i4);
}
}
结果:true;true;
解析:
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
4.谈谈Integer i = new Integer(xxx)和Integer i =xxx;这两种方式的区别。
答:1)第一种不会触发自动装箱操作,但是第二种会触发,
2)在执行效率和资源占用上的区别。第二种方式的执行效率和资源占用在一般性情况下要优于第一种情况(注意这并不是绝对的)。
5,下面程序的输出结果是什么?(注:当 "=="运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程))
public class BoxTest {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
Long h = 2L;
System.out.println(c==d);
System.out.println(e==f);
System.out.println(c==(a+b));
System.out.println(c.equals(a+b));
System.out.println(g==(a+b));
System.out.println(g.equals(a+b));
System.out.println(g.equals(a+h));
}
}
答案:
true
false
true
true
true
false
true
解析:第一个和第二个输出结果没有什么疑问。第三句由于 a+b包含了算术运算,因此会触发自动拆箱过程(会调用intValue方法),因此它们比较的是数值是否相等。而对于c.equals(a+b)会先触发自动拆箱过程,再触发自动装箱过程,也就是说a+b,会先各自调用intValue方法,得到了加法运算后的数值之后,便调用Integer.valueOf方法,再进行equals比较。同理对于后面的也是这样,不过要注意倒数第二个和最后一个输出的结果(如果数值是int类型的,装箱过程调用的是Integer.valueOf;如果是long类型的,装箱调用的Long.valueOf方法)。