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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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

?

不賣官司,直接給出輸出結果

true
true
false
true

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

?

2 第二波暴擊!!!

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

證據呢?為什么要這樣做?涉及到了什么知識點?Boolean,Byte,Short,Character,Long,Float,Double等有沒有同樣的情況?如果有的話,范圍分別是多少?

?

2.1 證據很好拿,debug一下

?

其中: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

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

?

看實現標準,官方指定的,猶如憲法

?

關于裝箱的轉換【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));} }

?

請問結果是什么?

?

正確答案是

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

?

如果能經受住上面三輪暴擊,看到最后的,你有成為優秀的程序員的潛質了,恭喜你了!

?

參考資料

【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

轉載于:https://www.cnblogs.com/davidwang456/p/11423996.html

總結

以上是生活随笔為你收集整理的java Integer中隐藏的细节魔鬼!来自面试官的三轮暴击!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。