Comparing a string with the empty string (Java)
问题 I have a question about comparing a string with the empty string in Java. Is there a difference, if I compare a string with the empty string with == or equals ? For example: String s1 = "hi"; if (s1 == "") or if (s1.equals("")) I know that one should compare strings (and objects in general) with equals , and not == , but I am wondering whether it matters for the empty string. 回答1: s1 == "" is not reliable as it tests reference equality not object equality (and String isn't strictly canonical)