爲了理解String對象是如何被創建和存儲的,我嘗試了下面的程序,並且看到了我有查詢的輸出。有人可以幫忙嗎?如何創建並存儲在Java中的字符串?
package corejava.immutable;
public class StringTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = "Omkar Patkar";
String s2 = "Omkar Patkar";
String s3 = "Omkar" + " Patkar";
String s4 = "Omkar";
String s5 = s4 +" Patkar";
String s6 = new String("Omkar Patkar");
System.out.println("Hashcode s1 = "+s1.hashCode());
System.out.println("Hashcode s2 = "+s2.hashCode());
System.out.println("Hashcode s3 = "+s3.hashCode());
System.out.println("Hashcode s4 = "+s4.hashCode());
System.out.println("Hashcode s5 = "+s5.hashCode());
System.out.println("Hashcode s6 = "+s6.hashCode());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Indentity Hashcode s1 = "+System.identityHashCode(s1));
System.out.println("Indentity Hashcode s2 = "+System.identityHashCode(s2));
System.out.println("Indentity Hashcode s3 = "+System.identityHashCode(s3));
System.out.println("Indentity Hashcode s4 = "+System.identityHashCode(s4));
System.out.println("Indentity Hashcode s5 = "+System.identityHashCode(s5));
System.out.println("Indentity Hashcode s6 = "+System.identityHashCode(s6));
System.out.println("Indentity Hashcode intern s6 = "+System.identityHashCode(s6.intern()));
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("s1 == s2 : - "+(s1 == s2));
System.out.println("s1 == s3 : - "+(s1 == s3));
System.out.println("s1 == s4 : - "+(s1 == s4));
System.out.println("s1 == s5 : - "+(s1 == s5));
System.out.println("s1 == s6 : - "+(s1 == s6));
System.out.println("\ns2 == s3 : - "+(s2 == s3));
System.out.println("s2 == s4 : - "+(s2 == s4));
System.out.println("s2 == s5 : - "+(s2 == s5));
System.out.println("s2 == s6 : - "+(s2 == s6));
System.out.println("\ns3 == s4 : - "+(s3 == s4));
System.out.println("s3 == s5 : - "+(s3 == s5));
System.out.println("s3 == s6 : - "+(s3 == s6));
System.out.println("\ns4 == s5 : - "+(s4 == s5));
System.out.println("s4 == s6 : - "+(s4 == s6));
System.out.println("\ns5 == s6 : - "+(s5 == s6));
System.out.println("\ns1 == s6.intern() : - "+(s1 == s6.intern()));
System.out.println("s2 == s6.intern() : - "+(s2 == s6.intern()));
System.out.println("s3 == s6.intern() : - "+(s3 == s6.intern()));
System.out.println("s4 == s6.intern() : - "+(s4 == s6.intern()));
System.out.println("s5 == s6.intern() : - "+(s5 == s6.intern()));
}
}
而且我能看到以下的輸出: - 的S5和S6
Hashcode s1 = 2062602683
Hashcode s2 = 2062602683
Hashcode s3 = 2062602683
Hashcode s4 = 76311326
Hashcode s5 = 2062602683
Hashcode s6 = 2062602683
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Indentity Hashcode s1 = 4337374
Indentity Hashcode s2 = 4337374
Indentity Hashcode s3 = 4337374
Indentity Hashcode s4 = 18019860
Indentity Hashcode s5 = 31054905
Indentity Hashcode s6 = 605645
Indentity Hashcode intern s6 = 4337374
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
s1 == s2 : - true
s1 == s3 : - true
s1 == s4 : - false
s1 == s5 : - false
s1 == s6 : - false
s2 == s3 : - true
s2 == s4 : - false
s2 == s5 : - false
s2 == s6 : - false
s3 == s4 : - false
s3 == s5 : - false
s3 == s6 : - false
s4 == s5 : - false
s4 == s6 : - false
s5 == s6 : - false
s1 == s6.intern() : - true
s2 == s6.intern() : - true
s3 == s6.intern() : - true
s4 == s6.intern() : - false
s5 == s6.intern() : - false
身份哈希碼是從S1,S2,S3 ......不同的爲什麼會這樣?
在其中的內存區域中,這些對象創建的? ...對象圖將有助於理解......
所有字符串預計將有一個空格之間......所以他們最後的值被假設是「OMKAR Patkar」 ...... S5和S6將包含類似S1的空間,S2和S3 ......但他們的身份hashcodes與s1,s2和s3不一樣......爲什麼有區別? –
關於字符串有很多問題,它們是如何創建和存儲的,包括字符串池。你是否搜索過並閱讀過它們? – Tom
湯姆...對不起,我看到很多鏈接討論這個話題......我的歉意。下次我會照顧的。 –