前言
基本類型直接存放在棧中,是以隻有包裝類常量池,
Java在jdk1.5後包裝類常量池使用緩存實作,本文使用1.8示範
由于字元串常量池有些特殊,我門先來了解包裝類常量池,也有助于接下來了解字元串常量池
包裝類常量池
基本概念
- Integer 、Long、Short、Byte、Character、Double、Float、Boolean八種基本類型中Double、Float沒有常量池,Boolean隻有true和value,其他5種基本相同。
- java使用緩存維護了一個 [-128,127] 的常量,正好是255個數,1個位元組byte所能表示最大值
以Integer為例,源碼分析 使用數組存放,最小值限定為-128,最大值可配置,但不能小于127,否則抛出錯誤。
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;
// 通過jvm擷取緩存最大值
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() {}
}
Integer 常量池
上代碼
Integer a = 128;
Integer b = 128;
Integer a2 = 127;
Integer b2 = Integer.valueOf(127);
System.out.println("1> a == b:" +(a == b));
System.out.println("2> a2 == b2:" + (a2 == b2));
Integer c = new Integer(128);
System.out.println("3> b=c:" + (b == c));
System.out.println("4> b=c.Intner:" + (b == Integer.valueOf(128)));
Integer d = 127;
Integer e = new Integer(127);
System.out.println("5> d==e:" + (d == e));
System.out.println("6> d = Integer.valueOf" + (d == Integer.valueOf(127)));
System.out.println("7> e = Integer.valueOf" + (e == Integer.valueOf(127)));
System.out.println("8> e.equals(d):" + e.equals(d));
// 執行結果
1> a == b:false
2> a2 == b2:true
3> b=c:false
4> b=c.Intner:false
5> d==e:false
6> d = Integer.valueOf true
7> e = Integer.valueOf false
8> e.equals(d):true
分析
1>使用=号指派,導緻強制拆包裝,檢視位元組碼檔案,可知調用了Integer.valueOf方法。

調用了Integer.valueOf方法,下面是該方法方法體,可知,如果緩存裡有該值,則傳回緩存裡對象,沒有則建立一個新的對象。因為Integer緩存存放[-128,127]之間的數,是以1>為false。
2> a2=127自動拆裝箱等于調用Integer.valueOf()方法,是以等于b2直接使用Integer.valueOf()方法
3> c是直接new對象,存放的是新的記憶體位址,b=128不在緩存範圍中,是以也是直接new Integer,兩個對象在記憶體中的位址不一樣,是以false。
4> 與3>情況類似,哈哈,🐶
5> d=127使用自動拆裝箱,會到緩存中擷取,e為new對象,是以不相同
6> 因為自動拆裝箱其實本質也是調用Integer.valueOf()方法,是以,為true
7> e為new對象,Integer.valueOf(127)是擷取緩存中的對象,是以false
8> 在阿裡java泰山版中,有這樣一段話
說明中已經說明了原因,我就不再贅述,我們一起來看一下equals方法
實際就是value和intValue進行比較,我們來看一下intValue()和value
其實就是Integer對象裡維護的final常量int值,該int值在對象被加載時,存放在方法區的元空間中(jdk1.8叫法),是以,equals比較的就是這兩個int值,不論是直接複制,new對象,調用Integer.valueOf方法,隻要是同一個數字,就都相等,是以為true.
字元串常量池
基本概念
- 為什麼要設計字元串常量池?因為字元串的建立要耗費大量的時間和空間,頻繁建立字元串影響程式性能。
- 建立(無論是直接指派,還是new對象)字元串時,首先會判斷字元串常量池是否有該字元串,如果有會直接引用,如果沒有才會執行個體化該字元串,并放入常量池
上代碼
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
String str3 = new String("abc");
String str4 = new String("abc");
String str5 = new String("def");
System.out.println("1> str1 == str2:" + (str1 == str2));
System.out.println("2> str3 == str4:" + (str3 == str4));
System.out.println("3> str1 == str3:" + (str1 == str3));
System.out.println("4> str1.equals(str3):" + str1.equals(str3));
System.out.println("5> str1 == str3.intern():" + (str1 == str3.intern()));
compareString(str1);
}
private static void compareString(String str0) {
String str = "abc";
System.out.println("6> str == str0:" + (str == str0));
}
// 執行結果
1> str1 == str2:true
2> str3 == str4:false
3> str1 == str3:false
4> str1.equals(str3):true
5> str1 == str3.intern():true
6> str == str0:true
分析
1> str1和str2都是壓棧了String abc,是以是相等的
2>str3和str4都是建立了新對象,是以是不相等的,但是其 本質還是都引用了常量池中的abc,5>就是證明。
3>str1是直接引用常量池,str3是先指向String對象再指向常量池,是間接引用,是以false
4>equals就是用來解決直接指向和間接指向的,由源碼可知其内部通過char進行逐個比較,全部相等則相等。
5>str1=str3.intern(),為啥就相等了呢?intern方法傳回的是什麼呢?
是以,使用intern方法,會擷取該字元串的引用。
這時,你可能有另外一個疑問,那會不會是在調用intern方法時才從字元串常量池中擷取引用,在new String()時,new的對象指向了另外一個字元串?
别着急,我來來看第6行列印。
6> 在調用compareString()時傳入了一個字元串常量池引用,在方法體中又建立了一個字元串常量池的引用,那肯定是true啊!有這個想法就對了,我們來看一下new String()時發生了什麼!
new String()也是傳入了一個字元串常量。
其内部實作就是将字元串常量value char數組引用指派了new對象中的value,是以new對象本質也是指向了字元串常量的引用位址。