今天課堂測試做了幾道String的練習題,做完直接心態爆炸......
整理自下面兩篇博客:
https://www.cnblogs.com/marsitman/p/11248001.html
https://www.cnblogs.com/aspirant/p/9193112.html
首先先來看看下面的代碼:
public class StringTest { public static void main(String[] args){ String s1="Hello"; String s2="Hello"; String s3=new String("Hello"); System.out.println("s1和s2 引用地址是否相同:"+(s1 == s2)); System.out.println("s1和s2 值是否相同:"+s1.equals(s2)); System.out.println("s1和s3 引用地址是否相同:"+(s1 == s3)); System.out.println("s1和s3 值是否相同:"+s1.equals(s3)); } }
打印結果如下:
s1和s2 引用地址是否相同:true s1和s2 值是否相同:true s1和s3 引用地址是否相同:false s1和s3 值是否相同:true
我們可以看到 在java中,比較String有兩種方式,一種是用"==",另一種是用s.equals()方法。
那么這兩種方法有什么不同呢?
上面程序中的"=="是判斷兩個對象引用的地址是否相同,也就是判斷是否為同一個對象,s1與s2 返回為true,s1與s3返回則是false。說明s1與s2 引用的同一個對象的地址,s3則與其它兩個引用不是同一個對象地址。
而s.equals()方法則是判斷字符串的內容是否相等,只要內容相等就返回true,當然,地址相等就更不用說了,肯定返回true。
Java為了避免產生大量的String對象,設計了一個字符串常量池。工作原理是這樣的,創建一個字符串時,JVM首先為檢查字符串常量池中是否有值相等的字符串,如果有,則不再創建,直接返回該字符串的引用地址,若沒有,則創建,然后放到字符串常量池中,並返回新創建的字符串的引用地址。所以上面s1與s2引用地址相同。
那為什么s3與s1、s2引用的不是同一個字符串地址呢?
注意看 s3的定義方法:
String s3=new String("Hello");
JVM首先是在字符串常量池中找"Hello" 字符串,如果沒有創建字符串常量,然后放到常量池中,若已存在,則不需要創建;當遇到 new 時,還會在內存(不是字符串常量池中,而是在堆里面)上創建一個新的String對象,存儲"Hello",並將內存上的String對象引用地址返回,所以s3與s1、s2引用的不是同一個字符串地址。 內存結構圖如下:
從內存圖可見,s1與s2指向的都是常量池中的字符串常量,所以它們比較的是同一塊內存地址,而s3指向的是堆里面的一塊地址,說的具體點應該是堆里面的Eden區域,s1跟s3,s2跟s3比較都是不相等的,都不是同一塊地址。
拓展問題:
請問String s = new String("xyz");產生了幾個對象?
在String的工作原理中,已經提到了,new一個String對象,是需要先在字符串常量中查找相同值或創建一個字符串常量,然后再在內存堆中創建一個String對象,所以String s = new String("xyz"); 會創建兩個對象。
下面看幾道習題:
1、
public class Test { public static void main(String[ ] args) { String s1 = new String("Welcome to Java!"); String s2 = new String("Welcome to Java!"); if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } }
s1 and s2 have different contents
雖然常量池中已經存在"Welcome to Java!"了,但s2 new 時,還會在內存(不是字符串常量池中,而是在堆里面)上創建一個新的String對象,存儲"Welcome to Java!",並將內存上的String對象引用地址返回給s2,所以s1和s2引用地址不同。
2、
public class Test { public static void main(String[ ] args) { String s1 = "Welcome to Java!"; String s2 = s1; if (s1 == s2) System.out.println("s1 and s2 reference to the same String object"); else System.out.println("s1 and s2 reference to different String objects"); } }
s1 and s2 reference to the same String object
常量池中已經存在"Welcome to Java!"了,不需要再創建,直接返回該字符串的引用地址給s2,所以s1和s2有相同的引用地址。
再來看這樣一個問題:
public class Test { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; String s3 = "helloworld"; System.out.println(s3 == s1 + s2);// false System.out.println(s3.equals((s1 + s2)));// true System.out.println(s3 == "hello" + "world");// ture System.out.println(s3.equals("hello" + "world"));// true } }
equals()比較方法不解釋,比較值,均相等,均為true。
- s1與s2相加是先在字符串常量池中開一個空間,然后拼接,這個空間的地址就是s1與s2拼接后的地址。與s3的地址不同,所以輸出為false。
- s3與”hello”+”world”作比較,”hello”+”world”先拼接成”helloworld”,然后再去字符串常量池中找是否有”helloworld”,有,所以和s3共用一個字符串對象,則為true。
總結:
String s = new String(“hello”)會創建2(1)個對象,String s = “hello”創建1(0)個對象。
注:當字符串常量池中有對象hello時括號內成立!
字符串如果是變量相加,先開空間,在拼接。
字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否則,就創建。