先看一道題目
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;System.out.PRintln(f1 == f2);System.out.println(f3 == f4);結果是 true、false
為什么恩,這要歸宿與JVM內存分配策略
方法區中運行時常量池存放常量。
什么是運行時常量池
運行時常量池(Runtime Constant Pool),它是方法區的一部分。Class文件中除了有類的版本、字段、方法、接口等描述等信息外,還有一項信息是常量池(Constant Pool Table),用于存放編譯期生成的各種字面量和符號引用,這部分內容將在類加載后存放到常量池中。
這里所說的常量包括:基本類型包裝類(包裝類不管理浮點型,整形只會管理-128到127)和String(也可以通過String.intern()方法可以強制將String放入常量池)。所以f3、f4的值沒有直接保存在運行時常量池中,而是新newInteger對象。== 是比較兩個值的內存地址是否一致,所以結果為false。
看看Integer源代碼,發現沒有
Integer f1 = 100,直接賦值的方法,那么它是如何做到的,我們通過反編譯工作查看源代碼發現,代碼已變為:Integer f1 = Integer.valueOf(100);Integer f1 = Integer.valueOf(150);在看看Integer.valueOf方法public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i);}IntegerCache類定義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) { 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); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {}}范圍超過~128到127,會新new一個Integer對象。Float、Double的值,沒有存放在運行時常量池中。而是存放在堆中。源碼反編譯,Flout、Double賦值后調用valueOf()方法Float f = Float.valueOf(0.2F); Double d = Double.valueOf(1.0D);-----------------------------------------public static Float valueOf(float f) { return new Float(f);}-----------------------------------------public static Double valueOf(double d) { return new Double(d);}-----------------------------------------都是新new一個對象,數據存放在堆中。總結一下Integer 值范圍在-128~127時,存放在運行時常量池中,超過范圍存放在堆中。Flout、Double 存放在堆中。引申問題:同樣是封裝類型 Integer、Float、Double三個人的待遇為什么會不一樣,其他兄弟都安安靜靜的待在運行時常量池,而它哥三卻這么不一樣?推測: 方法區中運行時常量池的內存大小所限,常量池中對基本數據類型和封裝類型的存放的變量值的大小有限制?上面得到結論:Float(4字節)、Double(8字節),Integer(4字節)保留了-128~127,等價于1字節。再看看:Byte(1字節)、Character(2字節)、Short(2字節)、Long(8字節)。查看Character、Short、Long源代碼中valueOf()方法發現值范圍超過-128~127,重新new一個對象。原因是這個嗎,有沒有其他原因恩?探究中。。。
新聞熱點
疑難解答