阅读本文大概需要6分钟
继续挖掘一些有趣的基础面试题,有错望指出来哈,请赐教~
简单一点说,装箱就是自动将基本数据类型转换为包装器类型
;拆箱就是自动将包装器类型转为基本数据类型
。
那它又是如何实现的?
以Integer为例,看下代码:
public class Box {
public static void main(String [] args){
Integer i = 10;//装箱
int n = i;//拆箱
}
}
反编译class文件之后得到如下内容:
D:\Study\java\jdk8\bin\javap.exe -c upBox.Box
Compiled from "Box.java"
public class upBox.Box {
public upBox.Box();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: bipush 10
2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
6: aload_1
7: invokevirtual #3 // Method java/lang/Integer.intValue:()I
10: istore_2
11: return
}
从反编译得到的字节码内容可以看出,在装箱
的时候自动调用的是Integer的valueOf(int)
方法。而在拆箱
的时候自动调用的是Integer的intValue
方法,其他的包装类类似。
深藏陷阱的面试题:
public class Test {
public static void main(String[] args) {
Integer f1 = 100;
Integer f2 = 100;
Integer f3 = 150;
Integer f4 = 150;
System.out.println(f1 == f2); //true
System.out.println(f3 == f4); //false
}
}
使用==对比的是引用是否相等,这很容易让人误认为两个输出都是true或false。
来分析一波:从上一题中可知道Integer装箱调用静态方法valueOf,我们来看下valueOf的源码
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可以看出,若int的值IntegerCache的最小值至最大值之间,则返回IntegerCache中的值,否则返回Integer对象;
IntegerCache是Integer缓存。接着看,IntegerCache源码
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
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() {}
}
IntegerCache 是Integer中的静态类,虚拟机加载Integer类就会将[-128,127]的值存储在Integer cache[]中。
由以上两段源码可得出:将int赋值给Integer时,若int的值在[-128,127]内,则会直接引用Intefer缓存池中的对象;不在,则创建新的Integer对象。
返回去看面试题,是否已一目了然?
import java.io.*;
import java.util.Date;
public class ObjectSaver {
public static void main(String[] args) throws Exception {
/*其中的 D:\\objectFile.obj 表示存放序列化对象的文件*/
//序列化对象
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\objectFile.obj"));
Customer customer = new Customer("张三", 25);
out.writeObject("hello!"); //写入字面值常量
out.writeObject(new Date()); //写入匿名Date对象
out.writeObject(customer); //写入customer对象
out.close();
//反序列化对象
ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\objectFile.obj"));
System.out.println("String:" + (String) in.readObject()); //读取字面值常量
System.out.println("Date:" + (Date) in.readObject()); //读取匿名Date对象
Customer obj3 = (Customer) in.readObject(); //读取customer对象
System.out.println("Customer:" + obj3.toString());
in.close();
}
}
class Customer implements Serializable {
private String name;
private int age;
public Customer(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "name=" + name + ", age=" + age;
}
}
执行结果: String:hello! Date:Sun Jul 08 11:38:17 GMT+08:00 2018 Customer:name=张三 age=25