java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!
1 第一波暴擊!!!
程序員比較實(shí)在,一般會(huì)說:
?
那就先上代碼
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));} }?
不賣官司,直接給出輸出結(jié)果
truetrue
false
true
錯(cuò)誤的去面壁吧!!!!!!!!!!!!!!
?
2 第二波暴擊!!!
有人會(huì)說:“這個(gè)我知道![-128,127]之間,指向已經(jīng)存在的對(duì)象的引用;否則創(chuàng)建一個(gè)新的Integer對(duì)象”
證據(jù)呢?為什么要這樣做?涉及到了什么知識(shí)點(diǎn)?Boolean,Byte,Short,Character,Long,Float,Double等有沒有同樣的情況?如果有的話,范圍分別是多少?
?
2.1 證據(jù)很好拿,debug一下
?
其中:static final int low = -128;high=127(間接)
2.2 這樣做的依據(jù)是什么?
源碼算嗎?
Integer SmallThan127=15;Integer biggerThan127=365;
通過調(diào)用包裝器的 Integer.valueOf方法實(shí)現(xiàn)的
/*** 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
還是不理解,不見棺材不落淚呀!
?
看實(shí)現(xiàn)標(biāo)準(zhǔn),官方指定的,猶如憲法
?
關(guān)于裝箱的轉(zhuǎn)換【1】
?
2.3 Boolean,Byte,Short,Character,Long,Float,Double這些看spec就行,還要我說嗎?
?
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));} }?
請問結(jié)果是什么?
?
正確答案是
falsetrue
false
true
為什么會(huì)這樣?
?
公布答案吧
3.1 ==的不同使用方法
?
==當(dāng)兩邊都是對(duì)象的時(shí)候,會(huì)比較對(duì)象;
==當(dāng)不都是對(duì)象時(shí),會(huì)進(jìn)行拆箱比較
3.2 equal時(shí),進(jìn)行值得比較,源碼如下:
/*** 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;}?
如果能經(jīng)受住上面三輪暴擊,看到最后的,你有成為優(yōu)秀的程序員的潛質(zhì)了,恭喜你了!
?
參考資料
【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
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/11423996.html
總結(jié)
以上是生活随笔為你收集整理的java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米亿级大数据实时分析与工具选型 【转】
- 下一篇: 菜鸟、普通、老鸟程序猿如何写奇数判断?-