12.09 JVM面試考點——值==問題(常量池)

基本類型的包裝類和常量池

java中基本類型的包裝類的大部分都實現了常量池技術,即Byte,Short,Integer,Long,Character,Boolean。

這5種包裝類默認創建了數值[-128,127]的相應類型的緩存數據,但是超出此範圍仍然會去創建新的對象。 兩種浮點數類型的包裝類Float,Double並沒有實現常量池技術。

Integer與常量池

Integer i1 = 40;
Integer i2 = 40;
Integer i3 = 0;
Integer i4 = new Integer(40);
Integer i5 = new Integer(40);
Integer i6 = new Integer(0);
System.out.println("i1=i2 " + (i1 == i2));
System.out.println("i1=i2+i3 " + (i1 == i2 + i3));
System.out.println("i1=i4 " + (i1 == i4));
System.out.println("i4=i5 " + (i4 == i5));
System.out.println("i4=i5+i6 " + (i4 == i5 + i6));
System.out.println("40=i5+i6 " + (40 == i5 + i6));
i1=i2 true
i1=i2+i3 true
i1=i4 false
i4=i5 false
i4=i5+i6 true
40=i5+i6 true

解釋:

  • (1)Integer i1=40;Java在編譯的時候會直接將代碼封裝成Integer i1=Integer.valueOf(40);,從而使用常量池中的對象。

  • (2)Integer i1 = new Integer(40);這種情況下會創建新的對象。

  • (3)語句i4 == i5 + i6,因為+這個操作符不適用於Integer對象,首先i5和i6進行自動拆箱操作,進行數值相加,即i4 == 40。然後Integer對象無法與數值進行直接比較,所以i4自動拆箱轉為int值40,最終這條語句轉為40 == 40進行數值比較。

String與常量池

String str1 = "abcd";
String str2 = new String("abcd");
System.out.println(str1==str2);//false
String str1 = "str";
String str2 = "ing";
String str3 = "str" + "ing";
String str4 = str1 + str2;
System.out.println(str3 == str4);//false
String str5 = "string";
System.out.println(str3 == str5);//true

解釋:

  • (1)new String("abcd")是在常量池中拿對象,"abcd"是直接在堆內存空間創建一個新的對象。只要使用new方法,便需要創建新的對象。

  • (2)連接表達式 +

    只有使用引號包含文本的方式創建的String對象之間使用“+”連接產生的新對象才會被加入字符串池中。

    對於所有包含new方式新建對象(包括null)的“+”連接表達式,它所產生的新對象都不會被加入字符串池中。

public static final String A; // 常量A
public static final String B; // 常量B
static {
A = "ab";
B = "cd";
}
public static void main(String[] args) {
// 將兩個常量用+連接對s進行初始化
String s = A + B;
String t = "abcd";
if (s == t) {
System.out.println("s等於t,它們是同一個對象");
} else {
System.out.println("s不等於t,它們不是同一個對象");
}
}

解釋:

s不等於t,它們不是同一個對象。

A和B雖然被定義為常量,但是它們都沒有馬上被賦值。在運算出s的值之前,他們何時被賦值,以及被賦予什麼樣的值,都是個變數。因此A和B在被賦值之前,性質類似於一個變量。那麼s就不能在編譯期被確定,而只能在運行時被創建了。

String s1 = new String("xyz"); //創建了幾個對象?

解釋:

考慮類加載階段和實際執行時。

  • (1)類加載對一個類只會進行一次。”xyz”在類加載時就已經創建並駐留了(如果該類被加載之前已經有”xyz”字符串被駐留過則不需要重複創建用於駐留的”xyz”實例)。駐留的字符串是放在全局共享的字符串常量池中的。

  • (2)在這段代碼後續被運行的時候,”xyz”字面量對應的String實例已經固定了,不會再被重複創建。所以這段代碼將常量池中的對象複製一份放到heap中,並且把heap中的這個對象的引用交給s1 持有。

這條語句創建了2個對象。

public static void main(String[] args) {
String s1 = new String("計算機");
String s2 = s1.intern();
String s3 = "計算機";
System.out.println("s1 == s2? " + (s1 == s2));
System.out.println("s3 == s2? " + (s3 == s2));
}
s1 == s2? false
s3 == s2? true

解釋:

String的intern()方法會查找在常量池中是否存在一份equal相等的字符串,如果有則返回該字符串的引用,如果沒有則添加自己的字符串進入常量池。

public class Test {public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.println((hello == "Hello") + " "); //true
System.out.println((Other.hello == hello) + " "); //true
System.out.println((other.Other.hello == hello) + " "); //true
System.out.println((hello == ("Hel"+"lo")) + " "); //true
System.out.println((hello == ("Hel"+lo)) + " "); //false
System.out.println(hello == ("Hel"+lo).intern()); //true
}
}
class Other {
static String hello = "Hello";
}
package other;
public class Other {
public static String hello = "Hello";
}

解釋:

在同包同類下,引用自同一String對象.

在同包不同類下,引用自同一String對象.

在不同包不同類下,依然引用自同一String對象.

在編譯成.class時能夠識別為同一字符串的,自動優化成常量,引用自同一String對象.

在運行時創建的字符串具有獨立的內存地址,所以不引用自同一String對象.

JVM解讀-方法區


個人介紹:

高廣超 :多年一線互聯網研發與架構設計經驗,擅長設計與落地高可用、高性能互聯網架構。

本文首發在 高廣超的簡書博客 (http://www.jianshu.com/u/2766e4cfc391)轉載請註明!


分享到:


相關文章: