Comparing String OBJECTS (only relevant to Java)

  1. Recall that Strings in Java are OBJECTS:

  2. Comparing "equality" of 2 String objects x1 and x2 :

    Test Outcome of the test
      x1 == x2     true if x1 and x2 are aliases  
      x1 != x2     true if x1 and x2 are not aliases  
      x1.equals(x2)     true if x1 and x2 are lexicographically equal  
      !(x1.equals(x2))     true if x1 and x2 are lexicographically different  
      x1.equalsIgnoreCase(x2)     true if x1 and x2 are lexicographically equal  
      while ignoring letter case (upper & lower case equal)

  3. Testing relative order of 2 String objects x1 and x2 :

    Test Outcome of test Meaning of Outcome
      x1.compareTo(x2)     < 0  
      > 0  
      == 0  
      x1 preceeds x2 lexicographically  
      x1 succeeds x2 lexicographically  
      x1 are x2 lexicographically equal  

    Example:
       "abc".compareTo("abcd")    returns a value < 0   
       "abcd".compareTo("abc")    returns a value > 0   
       "abc".compareTo("abc")     returns 0   
       "abc".compareTo("abd")     returns a value < 0   
       "abC".compareTo("abc")     returns a value < 0