Comparing strings in java

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

    The == operator is checking if the two OBJECTS are equal (are they references to the same object), not comparing the values containes therein.

    0 讨论(0)
  • 2020-12-22 06:45

    The == operator is comparing 2 references to the same string for equality.

    The String class has two methods that you should use to compare two strings for equality:

    String1.equals(String2);
    

    Returns true if String1 and String2 are identical (including the case of the letters).

    If you don't care about the case then you can use:

    String1.equalsIgnoreCase(String2);
    

    This returns true if String1 and String2 are equal regardless of the case (obviously).

    0 讨论(0)
  • 2020-12-22 06:48

    Java provides two basic mechanisms for testing for equality. The “==” operator can be used to test primitive values for equality, and can also be used to determine if two object references point to the same underlying object. For Java objects, the equals(Object) method will return true if the argument is equal to the object on which the method is invoked, where equality is defined by the object’s class semantics.

    Since Strings are objects, the equals(Object) method will return true if two Strings have the same contents, i.e., the same characters in the same order. The == operator will only be true if two String references point to the same underlying String object. Hence two Strings representing the same content will be equal when tested by the equals(Object) method, but will only by equal when tested with the == operator if they are actually the same object.

    Quoted from JavaTechniques:http://javatechniques.com/public/java/docs/basics/string-equality.html

    0 讨论(0)
  • 2020-12-22 06:54

    Lines 4, 5, and 6 are true because of the following lines in the method substring(int, int) of java.lang.String:

    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
    

    Because the substring you are requesting starts at 0 and has the length of the complete string you are simply returned a reference to the string itself so that a.substring(0) == a.

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题