在使用的过程中,我们是不需要手动进行转换的,那这个转换过程是谁来执行的呢?是编译器。
public static final Integer ONE = 1;
这条简单的语句,没啥毛病,但编译之后的class文件为:
public static final Integer ONE = Integer.valueOf(1);
而 valueOf 方法的实现为:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可见,虽然java提供了自动装箱和拆箱,但还是会消耗一点性能的。 |