Using '==' instead of .equals for Java strings [duplicate]
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What makes reference comparison (==) work for some strings in Java? I know this has been asked before, but in spite of recommendations to use .equals() instead of the == comparison operator, I found that == works all the time: String s1 = "Hello"; String s2 = "Hello"; System.out.println(s1 == s2); // true Can anyone give me an example of the == operator failing? 回答1: This is because you're lucky. The == operator