Java String Equals Method

Java String Equals

With this method, we check the equals of the strings based on their contents. In other words, we are checking whether or not the two strings have the same content.

So, let’s now do a program to understand this method. Here, we are going to create two objects, one with the help of a new keyword, and another with the help of a string literal, as we have discussed earlier some differences between both methods of creating a String object.

We will try invoking the equals() method. Keep in mind that this method takes string input for comparison. It returns true or false on the basis of whether the two strings are equal or not.

Just to mention that we are not talking about whether or not the two references point to the same object. We are just checking the content. If we want to check the references also, we need to compare the strings with help of the == operator, which we are going to see a little bit later. First, let’s concentrate on the equals method.

public class StringMethods {
public static void main(String[] args) {
String str1 = “Hello”;
String str2 = new String(“Hello”);
System.out.println(str1.equals(str2));
}
}

If we try executing the above program, we get an output as true. This makes sense right? Because str1 refers to the string ”Hello”, and str2 also refers to ”Hello”, and both contents are the same.

But this does not mean that these references are pointing to the same object. With our previous explanations based on what object is created where (in Heap and in SCP), we know that the first object got created in the SCP, and then another (with the new keyword) got created in the heap.

To check this, you can use the == operator, which helps us check the references.

public class StringMethods {
public static void main(String[] args) {
String str1 = “Hello”;
String str2 = new String(“Hello”);
System.out.println(str1.equals(str2));
System.out.println(str1 == str2);
}
}

The output for the above program is –

true

false

Well, we can see that using == operator gave false as output. This is because the str1 and str2 are NOT referencing the same object, but the different ones. Now, lets try the below program, and observe what is happening here.

public class StringMethods {
public static void main(String[] args) {
String str1 = “Hello”;
String str2 = new String(“Hello”);
String str3 = “Hello”;
System.out.println(str1 == str3);
}
}

So, in the above program, there are three objects. You now know where these objects are being created right? Here, executing this program would give output as true, since now the str1 and str3 are pointing to the same object, because of the fact that we know that in the SCP, if the object is already created with the given string literal, then don’t create a new object again, just share the references. So, I hope that you have now got the things.

One more thing that I want to mention here, which I’m going to explain through the following program →

public class StringMethods {
public static void main(String[] args) {
String str1 = “hello”;
String str2 = new String(“HELLO”);
System.out.println(str1.equals(str2));
}
}

We had seen just above that the equals method compares two strings on the basis of the contents. This time also, both the strings are Hello in general, just one is in the lowercase and another is in the uppercase, but the content is the same. If we run the program this time, we see the output as false.

So, just with a change in the case (uppercase and lowercase), but even the content being the same, things are changing here. The content is the same but we are receiving the output as false.

So, for this, we have another method, which is equalsIgnoreCase(). This method is similar to the equals method, but here, as the name of the method says, we are ignoring the case. Let’s now come to this method.