Difference between string object and string litera

2018-12-31 03:51发布

This question already has an answer here:

What is the difference between

String str = new String("abc");

and

String str = "abc";

13条回答
若你有天会懂
2楼-- · 2018-12-31 04:32

The following are some comparisons:

String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");

System.out.println(s1 == s2); //true
System.out.println(s1.equals(s2)); //true

System.out.println(s1 == s3);   //false
System.out.println(s1.equals(s3)); //true

s3 = s3.intern();
System.out.println(s1 == s3); //true
System.out.println(s1.equals(s3)); //true

When intern() is called the reference is changed.

查看更多
登录 后发表回答