摘要:還有需要注意的一點是,此類緩存行為不僅存在于對象。還存在于其他的整數(shù)類型,,,。但是能改變緩存范圍的就只有了。
前言
最近跟許多朋友聊了下,在這個“跳槽”的黃金季節(jié),大家都有點蠢蠢欲動,所以最近就多聊聊面試的時候需要注意的一些問題,這些問題不一定多深奧,多復(fù)雜,但是一不注意的話卻容易掉坑。下面看一下面試的時候經(jīng)常遇到的一段代碼:
public class IntegerDemo { public static void main(String[] args) { Integer numA = 127; Integer numB = 127; Integer numC = 128; Integer numD = 128; System.out.println("numA == numB : "+ (numA == numB)); System.out.println("numC == numD : "+ (numC == numD)); } }
根據(jù)大家以往的經(jīng)驗,會認(rèn)為上面的代碼用“==“符號來比較,對比的是對象的引用,那么ABCD是不同的對象,所以輸出當(dāng)然是false了。我在《“==”、“equals()”、“hashcode()”之間的秘密》這篇文章也討論過。那么事實也是如此嗎?下面看一下輸出結(jié)果:
numA == numB : true numC == numD : false
What?這個輸出結(jié)果怎么跟以往的認(rèn)知有所出入呢?在我們的代碼“Integer numA = 127”中,編譯器會把基本數(shù)據(jù)的“自動裝箱”(autoboxing)成包裝類,所以這行代碼就等價于“Integer numA = Integer.valueOf(127)”了,這樣我們就可以進(jìn)入valueOf方法查看它的實現(xiàn)原理。
//Integer valueOf方法 public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } //Integer靜態(tài)內(nèi)部類 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() {} }
從上面的源碼可以看到,valueOf方法會先判斷傳進(jìn)來的參數(shù)是否在IntegerCache的low與high之間,如果是的話就返回cache數(shù)組里面的緩存值,不是的話就new Integer(i)返回。
那我們再往上看一下IntegerCache,它是Integer的內(nèi)部靜態(tài)類,low默認(rèn)是-128,high的值默認(rèn)127,但是high可以通過JVM啟動參數(shù)XX:AutoBoxCacheMax=size來修改(如圖),如果我們按照這樣修改了,然后再次執(zhí)行上面代碼,這時候2次輸出都是true,因為緩存的區(qū)間變成-128~200了。
但是如果我們是通過構(gòu)造器來生成一個Integer對象的話,下面的輸出都是false。因為這樣不會走ValueOf方法,所以按照正常的對象對比邏輯即可。
public class IntegerDemo { public static void main(String[] args) { Integer numA = new Integer(127); Integer numB = new Integer(127); Integer numC = new Integer(128); Integer numD = new Integer(128); System.out.println("numA == numB : "+ (numA == numB));//false System.out.println("numC == numD : "+ (numC == numD));//false } }
還有需要注意的一點是,此類緩存行為不僅存在于Integer對象。還存在于其他的整數(shù)類型Byte,Short,Long,Character。但是能改變緩存范圍的就只有Integer了。
結(jié)語有時候往往越簡單的知識越容易掉坑里,所以要保持自己的求知欲,不斷鞏固的基礎(chǔ),才能讓自己在面試的時候不會栽跟頭。
文章始發(fā)于微信公眾號《深夜里的程序猿》,每天分享最干的干貨,轉(zhuǎn)載請注明出處,侵權(quán)必究。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/77509.html
摘要:清楚了以上流程,我們直接來看函數(shù)主要用作初始化應(yīng)用監(jiān)聽端口以及啟動。其中就是保存聊天室所有聊天消息的結(jié)構(gòu)。關(guān)于的解讀我會放到閱讀源碼時講。然后把消息加到緩存里,如果緩存大于限制則取最新的條消息。 tornado 源碼自帶了豐富的 demo ,這篇文章主要分析 demo 中的聊天室應(yīng)用: chatdemo 首先看 chatdemo 的目錄結(jié)構(gòu): ├── chatdemo.py ├── ...
摘要:類實際上是中中的緩存類,目的是節(jié)省內(nèi)存消耗,提高程序性能。而當(dāng)堆內(nèi)存中的對象存儲非常多時,就有可能造成內(nèi)存泄漏。使用頻率高創(chuàng)建對象也就越多,堆內(nèi)存中的對象也就越多,所以也就會可能發(fā)生上述中的內(nèi)存溢出等問題。 面試題:問以下代碼輸出的結(jié)果是多少? public class IntegerTest { @Test public void test() { ...
摘要:解決的中只有一個生命周期的鉤子也只有一句代碼報錯中的就是整個組建中的原來是寫在了使用指令的后面所以此時還沒有在組件中注冊所以會報錯誤正確代碼標(biāo)題這是一段內(nèi)容這是一段內(nèi)容這是一段內(nèi)容這是一段內(nèi)容。 用于記錄coding過程中遇到的比較難解決或者有意思的問題,包括前端/后端(Node/Db),會持續(xù)更新... 后端 Node redis集群模式下pipline報錯(2019.3.14) ...
摘要:解決的中只有一個生命周期的鉤子也只有一句代碼報錯中的就是整個組建中的原來是寫在了使用指令的后面所以此時還沒有在組件中注冊所以會報錯誤正確代碼標(biāo)題這是一段內(nèi)容這是一段內(nèi)容這是一段內(nèi)容這是一段內(nèi)容。 用于記錄coding過程中遇到的比較難解決或者有意思的問題,包括前端/后端(Node/Db),會持續(xù)更新... 后端 Node redis集群模式下pipline報錯(2019.3.14) ...
閱讀 3612·2023-04-25 16:35
閱讀 746·2021-10-11 11:09
閱讀 6274·2021-09-22 15:11
閱讀 3381·2019-08-30 14:03
閱讀 2617·2019-08-29 16:54
閱讀 3372·2019-08-29 16:34
閱讀 3104·2019-08-29 12:18
閱讀 2159·2019-08-28 18:31