equals() equalsIgnoreCase() String Example


Comparing Strings with equals() equalsIgnoreCase() String.

First and foremost important operation on strings is to compare whether two strings are same or not. There are three styles which the programmer should be well aware of when to use what. The styles are

  1. Using logical equals to (==)
  2. Using equals() and equalsIgnoreCase()
  3. Using lexicographical order, compareTo()

equals() vs ==

Programmer should be very very careful in choosing between equals() and ==, else may land in wrong outputs. The logical equals operator (==) in case of variables, either in Java or C/C++, compares the values stored in the locations (addresses), but in case of strings, the locations itself are compared. To be more clear, the logical equals operator in case of strings compare whether two strings refer the same location or not. If they refer, it evaluates to true, else false. The equals() method, in contrary, compares the values stored in the locations but not the locations itself. Read further notes.

public class ImmutableDemo
{
  public static void main(String args[])
  {
    String s1 = "lord";
    String s2 = "lord";

    String s3 = new String("lord");
    String s4 = new String("lord");

    System.out.println(s1 == s2);                   // true
    System.out.println(s3 == s4);                   // false    
    
    String s3 = new String("lord");
    String s4 = new String("lord");


    System.out.println(s1.equals(s2));              // true
    System.out.println(s3.equals(s4));              // true
	
    s2 = "world";
    System.out.println(s1 == s2); // false

    String s5 = "Lord";
    System.out.println(s1.equals(s5)); // false

    System.out.println(s1.equalsIgnoreCase(s5));    // true
    System.out.println(s1.length());                // prints 4
 }
}
equals() equalsIgnoreCase() String
Output screen of ImmutableDemo.java

String s1 = "lord";
String s2 = "lord";

In the above two statements, both s1 and s2 value (literal) is the same of
lord. Because the values are same, the JRE creates only one location for lord (in string pool) and is referred by both objects s1 and s2. This is done by JRE implicitly to save memory. We know earlier, in Java, the memory is implicitly managed; now further we can say, it is managed very effectively to the optimum level.

System.out.println(s1 == s2);

As s1 and s2 refer the same location (or value) in the pool, s1 == s2 evaluates to true. s1.equals(s2) returns true as the values of s1 and s2 are same. With equals() method, we do not use the word locations because equals() method compares the values but does not bother about locations whether both strings refer the same location or different locations.

String s3 = new String("lord");
String s4 = new String("lord");

In the above two statements, the JRE allocates different memory locations (and no way connected to string pool) for
s3 and s4 as programmer requested two diferent locations to allocate with new keyword. Observe, with s1 and s2, no
new keyword is used.

System.out.println(s3 == s4);

s3 == s4 returns false as both s3 and s4 refer different locations. But s3.equals(s4) returns true as equals() method compares the values but not locations.

System.out.println(s1.equalsIgnoreCase(s5));

equalsIgnoreCase() method does not bother about the case of the letters (equals() minds the case also).

As s1 and s5 refer the same word lord but their case is different, both are evaluated to true.

Many a times, programmer does not bother about the locations; he is advised to go for equals() method and not == operator as sometimes, even if the strings are same, the result may come false.

Note: It must be noted that equals() method is defined in Object class and is overridden by String class. Object class is the supermost class of all classes of Java and beyond which no Java class exist. But equalsIgnoreCase() is defined in String class itself.

Pass your comments and suggestions on this tutorial "equals() equalsIgnoreCase() String Example".

Leave a Comment

Your email address will not be published.