到目前为止,我一直在我的程序中使用==运算符来比较我的所有字符串。但是,我遇到了一个错误,将其中一个更改为.equals() ,然后修复了错误。 
是==坏?什么时候应该不应该使用它?有什么不同? 
 ==测试引用相等性(它们是否是同一个对象)。 
 .equals()测试值的相等性(它们在逻辑上是否 “相等”)。 
在调用.equals()之前, Objects.equals()会检查null ,因此您不必(从 JDK7 开始,也可以在Guava 中使用 )。 
 String.contentEquals()将String的内容与任何CharSequence的内容(自 Java 1.5 以来可用)进行比较。 
因此,如果要测试两个字符串是否具有相同的值,则可能需要使用Objects.equals() 。 
// These two have the same value
new String("test").equals("test") // --> true 
// ... but they are not the same object
new String("test") == "test" // --> false 
// ... neither are these
new String("test") == new String("test") // --> false 
// ... but these are because literals are interned by 
// the compiler and thus refer to the same object
"test" == "test" // --> true 
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true你几乎总是想使用Objects.equals() 。在极少数情况下,您知道您正在处理实习字符串,您可以使用== 。 
此外,字符串文字始终引用类
String的相同实例。这是因为字符串文字 - 或者更常见的是作为常量表达式(第15.28 节 )的值的字符串 - 被 “实例化” 以便使用String.intern方法共享唯一实例。
类似的例子也可以在JLS 3.10.5-1 中找到。
 ==测试对象引用, .equals()测试字符串值。 
有时它看起来好像==比较值,因为 Java 做了一些幕后的东西,以确保相同的内联字符串实际上是同一个对象。 
例如:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";但要注意空值!
 ==处理null字符串很好,但是从空字符串调用.equals()会导致异常: 
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));因此,如果您知道fooString1可能为 null,请通过编写告诉读者
System.out.print(fooString1 != null && fooString1.equals("bar"));以下内容较短,但检查 null(来自 Java 7)不太明显:
System.out.print(Objects.equals(fooString1, "bar")); ==比较对象引用。 
 .equals()比较 String 值。 
有时==会给出比较 String 值的幻想,如下列情况: 
String a="Test";
String b="Test";
if(a==b) ===> true这是因为当您创建任何字符串文字时,JVM 首先在字符串池中搜索该字面值,如果找到匹配项,则将为新字符串指定相同的引用。因此,我们得到:
(a == b)===> 是的
String Pool
     b -----------------> "test" <-----------------a但是,在以下情况下==失败: 
String a="test";
String b=new String("test");
if (a==b) ===> false在这种情况下,对于new String("test") ,将在堆上创建语句 new String,并且该引用将被赋予b ,因此b将在堆上给出引用,而不是在 String 池中。 
现在a指向 String 池中的 String,而b指向堆上的 String。因此我们得到: 
if(a == b)===> false。
String Pool
     "test" <-------------------- a
                   Heap
     "test" <-------------------- b虽然.equals()总是比较 String 的值,所以在两种情况下都给出了 true: 
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true所以使用.equals()总是更好。