日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

1 第一波暴擊!!!

程序員比較實在,一般會說:

?

那就先上代碼

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

錯誤的去面壁吧!!!!!!!!!!!!!!

?

2 第二波暴擊!!!

有人會說:“這個我知道![-128,127]之間,指向已經(jīng)存在的對象的引用;否則創(chuàng)建一個新的Integer對象”

證據(jù)呢?為什么要這樣做?涉及到了什么知識點?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方法實現(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

還是不理解,不見棺材不落淚呀!

?

看實現(xià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

為什么會這樣?

?

公布答案吧

3.1 ==的不同使用方法

?

==當兩邊都是對象的時候,會比較對象;

==當不都是對象時,會進行拆箱比較

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;}

?

如果能經(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)容還不錯,歡迎將生活随笔推薦給好友。