08_==和equals()

使用 == 和 equals() 比较字符串。

String 中 == 比较引用地址是否相同,equals() 比较字符串的内容是否相同:

String s1 = "Hello";              // String 直接创建
String s2 = "Hello";              // String 直接创建
String s3 = s1;                   // 相同引用
String s4 = new String("Hello");  // String 对象创建
String s5 = new String("Hello");  // String 对象创建
 
s1 == s1;         // true, 相同引用
s1 == s2;         // true, s1 和 s2 都在公共池中,引用相同
s1 == s3;         // true, s3 与 s1 引用相同
s1 == s4;         // false, 不同引用地址
s4 == s5;         // false, 堆中不同引用地址
 
s1.equals(s3);    // true, 相同内容
s1.equals(s4);    // true, 相同内容
s4.equals(s5);    // true, 相同内容

  

https://www.runoob.com/java/java-string-equals.html

https://blog.csdn.net/caoxuecheng001/article/details/88804286

原文地址:https://www.cnblogs.com/Thui/p/15785269.html