Comparing strings in java

前端 未结 7 1496
别那么骄傲
别那么骄傲 2020-12-22 05:58
String string1 = \"Hi there\";
String string2 = \"Hi\";
String string3 = \"Hi\";

System.out.println(string1.substring(0, string2.length()) == string2); //1
System.o         


        
7条回答
  •  清酒与你
    2020-12-22 06:55

    If you want to compare Strings, then you should use String.equals() (or String.equalsIgnoreCase()) method. Comparing by == tells you only if two references points to same object. And in your example that's it: string2 and string3 points to same instance of string "Hi" (why should Java create two exactly same strings given in compile-time)

提交回复
热议问题