日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/4/5 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击! 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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é)果

true
true
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é)果是什么?

?

正確答案是

false
true
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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。