Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intern()返回值错误 #210

Open
YPEU opened this issue Nov 20, 2023 · 0 comments
Open

intern()返回值错误 #210

YPEU opened this issue Nov 20, 2023 · 0 comments

Comments

@YPEU
Copy link

YPEU commented Nov 20, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant