-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Open
Description
P114页,8.12.1(常量池中已存在字符串)
以下代码的执行结果:
书上:false,true
idea(JDK8,JDK21):false,false
`
@test
public void internTest(){
String s1 = new String("1");
s1.intern();
String s2 = "1";
System.out.println("s1 == s2 : " + (s1 == s2));
System.out.println("=========================");
String s3 = "1" + "1";
s3.intern();
String s4 = "11";
System.out.println("s3 == s4 : " + (s3 == s4));
}
`
修正代码:
s3.intern()——>s3 = s3.intern();
`
@test
public void internTest(){
String s1 = new String("1");
s1.intern();
String s2 = "1";
System.out.println("s1 == s2 : " + (s1 == s2));
System.out.println("=========================");
String s3 = "1" + "1";
s3 = s3.intern();
String s4 = "11";
System.out.println("s3 == s4 : " + (s3 == s4));
}
`
此时打印结果为:true false
Metadata
Metadata
Assignees
Labels
No labels