1 第一波暴击!!!

程序员比较实在,一般会说:

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

那就先上代码

package com.example.demo;
public class TestInteger {
public static void main(String[] args) {
Integer SmallThan127=15;
Integer anotherSmallThan127=15;
System.out.println(SmallThan127==anotherSmallThan127);
System.out.println(SmallThan127.equals(anotherSmallThan127)); Integer biggerThan127=365;
Integer anotherBiggerThan127=365;
System.out.println(anotherBiggerThan127==biggerThan127);
System.out.println(anotherBiggerThan127.equals(biggerThan127));
}
}

不卖官司,直接给出输出结果

true
true
false
true

错误的去面壁吧!!!!!!!!!!!!!!

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

2 第二波暴击!!!

有人会说:“这个我知道![-128,127]之间,指向已经存在的对象的引用;否则创建一个新的Integer对象”

证据呢?为什么要这样做?涉及到了什么知识点?Boolean,Byte,Short,Character,Long,Float,Double等有没有同样的情况?如果有的话,范围分别是多少?

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

2.1 证据很好拿,debug一下

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

其中:static final int low = -128;high=127(间接)

2.2 这样做的依据是什么?

源码算吗?

Integer SmallThan127=15;
Integer biggerThan127=365;

通过调用包装器的 Integer.valueOf方法实现的

 /**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @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);
}

这就是传说中的装箱boxing

还是不理解,不见棺材不落泪呀!

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

看实现标准,官方指定的,犹如宪法

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

关于装箱的转换【1】

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

2.3 Boolean,Byte,Short,Character,Long,Float,Double这些看spec就行,还要我说吗?

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

3 第三波暴击!!!

    public class TestInteger {
public static void main(String[] args) {
Integer SmallThan127=new Integer(15);
Integer anotherSmallThan127=new Integer(15);
System.out.println(SmallThan127==anotherSmallThan127);
System.out.println(SmallThan127.equals(anotherSmallThan127)); Integer biggerThan127=new Integer(365);
Integer anotherBiggerThan127=new Integer(365);
System.out.println(anotherBiggerThan127==biggerThan127);
System.out.println(anotherBiggerThan127.equals(biggerThan127));
}
}

请问结果是什么?

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

正确答案是

false
true
false
true

为什么会这样?

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

公布答案吧

3.1 ==的不同使用方法

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

==当两边都是对象的时候,会比较对象;

==当不都是对象时,会进行拆箱比较

3.2 equal时,进行值得比较,源码如下:

 /**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

如果能经受住上面三轮暴击,看到最后的,你有成为优秀的程序员的潜质了,恭喜你了!

java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!-LMLPHP

参考资料

【1】https://docs.oracle.com/javase/specs/jls/se12/html/jls-5.html#jls-5.1.7

【2】https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.25

05-11 17:28