首先,
String s = newString("abs");它将创建两个对象,一个在池区域中的对象,另一个在非池区域中的对象,因为您将使用new和字符串文字作为参数。String str1 = "Hello";String str2 = "World";String str3 = new String("HelloWorld");String str4 = str1 + str2;到现在为止,您有五个String对象,其中四个在String Constant Pool中,另一个在Heap中。因此,您的str4完全是String
Pool中的一个新对象,请同时检查以下代码,
String str5="HelloWorld"; //This line will create one more String Constant Pool object because we are using the variable name as str5. String str6="HelloWorld";////This line will not create any object, this will refer the same object str5.
进行测试
System.out.println(str3==str4); //falseSystem.out.println(str4==str5);//falseSystem.out.println(str5==str6);//true



