av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java Integer.ValueOf()的一些了解

瀏覽:78日期:2022-09-02 08:34:23

本文是對(duì) Integer.ValueOf()的一些了解,分享給大家

Java Integer.ValueOf()的一些了解

這道題有的人或許做過(guò),也可能選對(duì),但是這其中的道理你卻不一定理解,在這里大牛走過(guò),小白留下一起學(xué)習(xí)。

Java Integer.ValueOf()的一些了解

先來(lái)分析選型A,Integer i01 = 59,是一個(gè)裝箱的過(guò)程,在進(jìn)行i01 == i02的比較過(guò)程中,因?yàn)橛疫吺钦停l(fā)生了拆箱的動(dòng)作,所以進(jìn)行了值得比較,所以返回true。

在這里拿出Integer a = 59,Integer b = 59,這種又會(huì)出現(xiàn)什么狀況呢,如果按照裝箱和拆箱來(lái)看就是true,如果按照對(duì)象來(lái)看,就是false,在你舉棋不定得時(shí)候你就應(yīng)該看看源碼了。

/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue =sun.misc.VM.getSavedProperty('java.lang.Integer.IntegerCache.high'); if (integerCacheHighPropValue != null) {try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1);} catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it.} } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }

這個(gè)類是Integer類中的一個(gè)靜態(tài)內(nèi)部類,其中的靜態(tài)代碼塊在類進(jìn)行加載的時(shí)候就進(jìn)行了-127-128這些數(shù)字的創(chuàng)建和保存,將他們的引用全部保存在Cache數(shù)組中。

所以當(dāng)用Integer 聲明初始化變量時(shí),會(huì)先判斷所賦值的大小是否在-128到127之間,若在,則利用靜態(tài)緩存中的空間并且返回對(duì)應(yīng)cache數(shù)組中對(duì)應(yīng)引用,存放到運(yùn)行棧中,而不再重新開辟內(nèi)存。

這里你就懂了吧,Integer a = 59,Integer b = 59返回的就是true,Integer a = 300,Integer b = 300在判斷完之后就會(huì)new出來(lái)一個(gè)新的對(duì)象,所以會(huì)返回false。

Java Integer.ValueOf()的一些了解

我們來(lái)分析B選項(xiàng),我們先來(lái)看Value的代碼。

* @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);

和上面的一樣,int進(jìn)去之后首先進(jìn)行判斷,如果在-128-127之間就會(huì)返回引用,否則就在堆上new出來(lái)對(duì)象。所以B選項(xiàng)返回true。

Java Integer.ValueOf()的一些了解

C選項(xiàng)i03返回的是Cache數(shù)組中的引用,而i04返回的是堆上對(duì)象的引用,所以返回的是false。

Java Integer.ValueOf()的一些了解

 System.out.println(i02== i04) i02是整型變量,i04是引用,這里又用到了解包,虛擬機(jī)會(huì)把i04指向的數(shù)據(jù)拆箱為整型變量再與之比較,所以比較的是數(shù)值,59==59,返回true.

到此這篇關(guān)于Java Integer.ValueOf()的一些了解的文章就介紹到這了,更多相關(guān)Java Integer.ValueOf()內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 日韩一区二区三区在线观看视频 | 啪视频在线 | 欧美激情精品久久久久久 | 国产一区在线视频 | 一区二区福利视频 | 一级h片 | a欧美| 国产午夜精品视频 | 又黑又粗又长的欧美一区 | 天天搞天天搞 | 男人天堂av网站 | 成人免费视频网站在线看 | 国产精品国产精品国产专区不蜜 | 激情六月丁香婷婷 | 在线亚洲人成电影网站色www | 看av片网站| 成人av免费在线观看 | 日本久久一区二区三区 | 精品一区二区电影 | 国产精品av久久久久久久久久 | 人人玩人人添人人澡欧美 | 91免费小视频 | 国产精品国产精品国产专区不蜜 | 精品在线播放 | 亚洲精品乱码久久久久久按摩观 | 色悠悠久 | 在线三级网址 | 久久午夜精品 | 日韩av在线一区 | 欧美一区在线看 | 日韩久久久久久 | 欧美一区二区三区一在线观看 | 亚洲国产精品视频 | 91久久久久久 | 成人免费小视频 | 一区二区三区免费 | 成人不卡一区二区 | 中文字幕精品一区二区三区精品 | 久久久久久高潮国产精品视 | 久草综合在线 | 欧美日韩一区二区在线播放 |