Comparing strings in java

前端 未结 7 1481
别那么骄傲
别那么骄傲 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:57

    The == compare the reference - the address of the string not the value of it. For comparing strings you should use equals. The JVM will handle new String objects, so if an object of the same value exists (string2 vs string3) it might reference the same one.

提交回复
热议问题