62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args){
String str1 = new String("abc") + new String("abc");
str1.intern();
String str2 = "abcabc";
System.out.println(str1 == str2);//true
String str3 = new String("abcd") + new String("abcd");
String str4 = "abcdabcd";
str3.intern();
System.out.println(str3 == str4);//false
}
public static void main(String[] args){
String str2 = new String("hello");
str2.intern();
String str = "hello";
System.out.println(str==str2);// 运行后结果为false
String str3 = new String("world");
String str4 = "world";
str3.intern();
System.out.println(str4==str3);// 运行后结果为false
}
@Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
public static void main(String[] args){
String str1 = new String("abc") + new String("abc");
str1.intern();
String str2 = "abcabc";
System.out.println(str1 == str2);//true
String str3 = new String("abcd") + new String("abcd");
String str4 = "abcdabcd";
str3.intern();
System.out.println(str3 == str4);//false
}
public static void main(String[] args){
//堆内存:
//POOL:
String str1 = new String(new char[]{'a','b','c','a','b','c'});
//堆内存:str1@&01, str1.value={'a','b','c','a','b','c'}@&02, str1.hashCode=-1424388928
//POOL:
str1.intern(); //pool中没有,所以放到pool中,(注意:返回值为&01)
//堆内存:str1@&01, str1.value={'a','b','c','a','b','c'}@&02, str1.hashCode=-1424388928
//POOL:hashCode:-1424388928⇒&01
String str2 = "abcabc"; //pool中有,所以用pool的
//堆内存:str2&01, str2.value={'a','b','c','a','b','c'}@&02, str2.hashCode=-1424388928
//POOL:hashCode:-1424388928⇒&01
System.out.println(str1 == str2);// &01==&01,true
//不再考虑上面的堆内存和pool
//堆内存:
//POOL:
String str3 = new String(new char[]{'a','b','c','d','a','b','c','d'});
//堆内存:str3@&03, str3.value={'a','b','c','d','a','b','c','d'}@&04, str1.hashCode=1259550596
//POOL:
String str4 = "abcdabcd"; //pool中没有,所以放到pool中
//堆内存:
// str3@&03, str3.value={'a','b','c','d','a','b','c','d'}@&04, str1.hashCode=1259550596
// str4@&05, str4.value={'a','b','c','d','a','b','c','d'}@&06, str1.hashCode=1259550596
//POOL:1259550596⇒&05
str3.intern(); //pool已有,不做任何操作 (注意:返回值为&05,如果str3=str3.intern(),则结果不一样)
// str3@&03, str3.value={'a','b','c','d','a','b','c','d'}@&04, str1.hashCode=1259550596
// str4@&05, str4.value={'a','b','c','d','a','b','c','d'}@&06, str1.hashCode=1259550596
//POOL:1259550596⇒&05
System.out.println(str3 == str4); &03==&05//false
}
public static void main(String[] args){
String str2 = new String("hello");
str2.intern();
String str = "hello";
System.out.println(str==str2);// 运行后结果为false
String str3 = new String("world");
String str4 = "world";
str3.intern();
System.out.println(str4==str3);// 运行后结果为false
}
public static void main(String[] args){
//堆内存:
//POOL:
String temp = "hello"; //pool中没有,所以放到pool中
//堆内存:temp@&10,temp.value={'h','e','l','l','o'}@&11, temp.hashCode=99162322
//POOL:99162322⇒&10
String str2 = new String(temp); //str2的value存储到temp的引用,并不分配新地址
//堆内存:
// temp@&10,temp.value={'h','e','l','l','o'}@&11, temp.hashCode=99162322
// str2@&12,str2.value={'h','e','l','l','o'}@&11, str2.hashCode=99162322
//POOL:99162322⇒&10
str2.intern(); //pool已有,不做任何操作 (注意:返回值为&10,如果str2=str2.intern(),则结果不一样)
// temp@&10,temp.value={'h','e','l','l','o'}@&11, temp.hashCode=99162322
// str2@&12,str2.value={'h','e','l','l','o'}@&11, str2.hashCode=99162322
//POOL:99162322⇒&10
String str = "hello";//pool已有,所以用pool的
// temp@&10,temp.value={'h','e','l','l','o'}@&11, temp.hashCode=99162322
// str2@&12,str2.value={'h','e','l','l','o'}@&11, str2.hashCode=99162322
// str@&10,str.value={'h','e','l','l','o'}@&11, str.hashCode=99162322
//POOL:99162322⇒&10
System.out.println(str==str2);// &10==&12 运行后结果为false
//不再考虑上面的堆内存和pool
//堆内存:
//POOL:
String temp1 = "world" //pool中没有,所以放到pool中
//堆内存:temp1@&20,temp.value={'w','o','r','l','d'}@&21, temp.hashCode=113318802
//POOL:113318802⇒&20
String str3 = new String(temp1); //str3的value存储到temp1的引用,并不分配新地址
//堆内存:
// temp1@&20,temp.value={'w','o','r','l','d'}@&21, temp.hashCode=113318802
// str3@&22,str3.value={'w','o','r','l','d'}@&21, str3.hashCode=113318802
//POOL:113318802⇒&20
String str4 = "world";//pool已有,所以用pool的
//堆内存:
// temp1@&20,temp.value={'w','o','r','l','d'}@&21, temp.hashCode=113318802
// str3@&22,str3.value={'w','o','r','l','d'}@&21, str3.hashCode=113318802
// str4@&20,str4.value={'w','o','r','l','d'}@&21, str4.hashCode=113318802
//POOL:113318802⇒&20
str3.intern();//pool已有,不做任何操作 (注意:返回值为&20,如果str3=str3.intern(),则结果不一样)
// temp1@&20,temp.value={'w','o','r','l','d'}@&21, temp.hashCode=113318802
// str3@&22,str3.value={'w','o','r','l','d'}@&21, str3.hashCode=113318802
// str4@&20,str4.value={'w','o','r','l','d'}@&21, str4.hashCode=113318802
//POOL:113318802⇒&20
System.out.println(str4==str3);// &20==&22 运行后结果为false
}