【Java学习】Integer.ValueOf()引发的血案

Laurie ·
更新时间:2024-09-20
· 739 次阅读

 这道题有的人或许做过,也可能选对,但是这其中的道理你却不一定理解,在这里大牛走过,小白留下一起学习。

先来分析选型A,Integer i01 = 59,是一个装箱的过程,在进行i01 == i02的比较过程中,因为右边是整型,发生了拆箱的动作,所以进行了值得比较,所以返回true。

在这里拿出Integer a = 59,Integer b = 59,这种又会出现什么状况呢,如果按照装箱和拆箱来看就是true,如果按照对象来看,就是false,在你举棋不定得时候你就应该看看源码了。

/** * 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=} 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 = 127; } private IntegerCache() {} }

这个类是Integer类中的一个静态内部类,其中的静态代码块在类进行加载的时候就进行了-127-128这些数字的创建和保存,将他们的引用全部保存在Cache数组中。

所以当用Integer 声明初始化变量时,会先判断所赋值的大小是否在-128到127之间,若在,则利用静态缓存中的空间并且返回对应cache数组中对应引用,存放到运行栈中,而不再重新开辟内存。

这里你就懂了吧,Integer a = 59,Integer b = 59返回的就是true,Integer a = 300,Integer b = 300在判断完之后就会new出来一个新的对象,所以会返回false。

我们来分析B选项,我们先来看Value的代码。

* @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }

和上面的一样,int进去之后首先进行判断,如果在-128-127之间就会返回引用,否则就在堆上new出来对象。所以B选项返回true。

C选项i03返回的是Cache数组中的引用,而i04返回的是堆上对象的引用,所以返回的是false。

System.out.println(i02== i04) i02是整型变量,i04是引用,这里又用到了解包,虚拟机会把i04指向的数据拆箱为整型变量再与之比较,所以比较的是数值,59==59,返回true.


作者:小白又开始整活了



java学 valueof java学习 JAVA INTEGER

需要 登录 后方可回复, 如果你还没有账号请 注册新账号