Artem Divertitto
Senior Android Developer at United Tech

Java String equals()

Published in the Java Developer group
Comparing objects for equivalence is one of the most important programming operations. However, for all its obviousness in the numeric world, it is not always clear how to compare other data types. Java Object class, a basic one, defines the equals() and compareTo() methods for comparison. String class overrides its equals() method. Java String equals() method compares two strings according to their contents. Java String equals() - 1String equals() method signature looks like this:

public boolean equals(Object anotherObject) 
String equals() method compares the string to the specified object. If the strings are equal, it returns true, otherwise it’s false. You might be wondering why not compare strings using the == comparison operator? In fact, this is also possible, but the result will be somewhat different. The fact is that the == operator compares addresses in memory. So if s1 == s2 returns true, these two strings have the same address in memory. At the same time, equals checks the contents of strings for equality. Let's take an example of comparing two strings.

public class StringEqualsTest {
   //program to test Java equals() method
   public static void main(String[] args) {
       String myString1 = "here is my favorite string";
       String myString2 = "here is my favorite string"; //this string is the same as the previous one, at least we think so 
       String myString3 = "here is My favorite string"; //this string looks a little bit like previous two, but the first letter is big M instead of small m       
       String myString4 = new String("here is my favorite string");
//here we create a String in an object manner… Why? Find out soon  
       String myString5 = "equals to myString1? No, not at all..."; //here we have absolutely different string. Just for fun 
    //let’s compare myString1 with myString2 using “==” operator  
       System.out.println(myString1 == myString2); //true
    //let’s compare myString1 with myString4 using “==” operator  
       System.out.println(myString1 == myString4); //false    
//and now we are going to use equals() method to compare myString1 with myString4, myString2 and myString5  
     System.out.println(myString1.equals(myString4));//false
       System.out.println(myString1.equals(myString2));//true
       System.out.println(myString1.equals(myString5)); //false

   }
}
The output of this program is:
true false true true false
Let's take a closer look at what's going on here. When we create a string, it is placed in the string pool, a special area of memory. Strings take up a huge portion of all objects in any large program. So to save memory String Pool was created, where a string with the text you need is placed. Later newly created links refer to the same memory area, there is no need to allocate additional memory each time. If you create a string without new operator, that is, when you write

String  myStringName = "...........................................", 
or something like that, the program checks if there is a String with such text in the String pool. If it is, no new String will be created. And the new link will point to the same address in the String Pool where that String is already stored. So when we wrote in the program

 String myString1 = "here is my favorite string";
 String myString2 = "here is my favorite string";
the reference myString1 points to exactly the same place in memory as myString2. The first command created a new string in the String Pool with the text we needed, and when it came to the second, it simply referred to the same memory area as myString1. But the string myString4 was created as an object using new operator. This operator allocates a new area in memory for an object when it is created. A string created with new

 String myString1 = new String ("here is my favorite string");
 String myString2 = new String ("here is my favorite string");
doesn’t fall into the String Pool, but becomes a separate object, even if its text completely matches the same string from the String Pool. Moreover, if we compare strings using String equals() method, it will check not the address, but the contents of the string, the sequence of characters in the strings. And if the text in the strings is the same, it doesn't matter how they were created and where they are stored, in the String Pool, or in a separate memory area. That's why myString1, myString2 and myString4 were equal in this comparison. By the way, have you noticed that String equals() method allows you to correctly compare strings in a case-sensitive manner? That is, if we compare the string "my string" with the strings "My string" and "MY STRING" we get false.

public class StringEqualsTest {

   public static void main(String[] args) {

       String myString1 = new String ("here is my favorite string");
       String myString2 = new String ("here is My favorite string");
       String myString3 = new String("HERE IS MY FAVORITE STRING");
      
       System.out.println(myString1.equals(myString2)); //false because first string myString1 has small m and myString2 has big M instead 
       System.out.println(myString1.equals(myString3));//false because myString1 is in lowercase while myString3 is in uppercase

   }
}
The output is here:
false false
Java String equals() - 2To compare strings in a case insensitive way, Java has a method that is very similar to equals:

boolean equalsIgnoreCase​(String anotherString)
Let's use it in our example.

public class StringEqualsTest {

   public static void main(String[] args) {

       String myString1 = new String ("here is my favorite string");
       String myString2 = new String ("here is My favorite string");
       String myString3 = new String("HERE IS MY FAVORITE STRING");
     /* here we are going to use the brother of equals() method, equalsIgnoreCase(). It can help to check user input when case isn’t 
important */ 
       System.out.println(myString1.equalsIgnoreCase(myString2));

       System.out.println(myString1.equalsIgnoreCase(myString3));

   }
}
Now the output is:
true true
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
matemate123 Level 28, Kraków, Poland
16 January 2023
I think in first example of code is mistake. In 15 line


  System.out.println(myString1.equals(myString4));//false

It’s true and bellow in output is correctly true