我运行了以下程序,
String firstString = "String";
String secondString = "String";
String thirdString = new String("String");
System.out.println(firstString == secondString);
System.out.println(firstString == thirdString);
System.out.println(firstString.intern() == thirdString);
System.out.println(firstString.intern() == thirdString.intern());
System.out.println(firstString.intern().equals(thirdString.intern()));
System.out.println(firstString == thirdString);
我的输出是
true
false
false
true
true
false
我了解到 jvm 将字符串与相同的字符串具有相同的内容。那正确吗?如果那是真的,那么为什么 firstString == thirdString 不返回假?jvm是否只汇集仅使用 :"" 而不是使用 new 运算符初始化的字符串?