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

判断定义为String类型的s1和s2是否相等? #36

Open
strongant opened this issue Mar 31, 2017 · 1 comment
Open

判断定义为String类型的s1和s2是否相等? #36

strongant opened this issue Mar 31, 2017 · 1 comment

Comments

@strongant
Copy link
Owner

String s1 = new String("abc");            
String s2 = "abc";
System.out.println(s1 == s2);        
System.out.println(s1.equals(s2));
@strongant
Copy link
Owner Author

strongant commented Mar 31, 2017

String s1 = new String("abc");    //s1指向堆内存对象的地址值        
String s2 = "abc"; //s2指向常量池中对象的地址值
System.out.println(s1 == s2);        //地址不相同,打印false
System.out.println(s1.equals(s2)); //比较内容,不关注对象引用地址,打印true

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