Understanding the Java String Equals Method

java string equalsIn Java, strings are dealt with in a different manner unlike C and C++ where strings were represented by a character type array. In Java, strings are created and manipulated using objects of String class. This allows developers and API programmers to associate several utility methods with this string type object. One such method is the Equals method.

To further explore Java Strings, take a course at Udemy.com.

What is Java String Equals Method?

Java String Equals method is an instance method that allows comparing two string objects or literal for equality. The String Equals method is called on the String type object that has to be compared for equality. The string object to which the string has to be compared is passed as a parameter to the Equals method. The return type for the Equals method is Boolean. If two strings that are being compared for equality contain the same character sequence, the Equals method returns true. Otherwise, it’s false.

The definition of Equals method in String class looks like this:

public bool equals(String str)

You can see from the definition that the “equals” method would return a Boolean value true or false and will accept a parameter of type String.

How the Java String Equals Method Works

The following example demonstrates how the “equals” method works in Java:

String str = "First String";

String str2 = "First String";

boolean result = str.equals(str2);

System.out.println("Are strings str and str2, equal?: "+result);

In the above example, two string objects str and str2 are compared using “equals” method. Both the objects contain the same character sequence of “First String”. Therefore, the comparison of these two strings via “equals” method yields true.

On the other hand, if two strings compared using the “equals” method contain a different character sequence, the returned Boolean variable contains a false value. This is shown in the following example:

String str = "First String";

String str2 = "Second String";

boolean result = str.equals(str2);

System.out.println("Are strings str and str2, equal?: "+result);

In the above example, String objects str and str2 contains two different character sequences i.e. “First String” and “Second String” respectively. When these strings are compared using the “equals” method and the result is displayed on the console, the value false is displayed. The console output of the above example is:

“Are strings str and str2, equal?: false”

Apart from comparing String type objects, the “equals” method can also be called on string literals. This concept has been explained in the following example:

boolean result = "First String".equals("First String");

System.out.println("Are strings str and str2, equal?: "+result);

result = "First Stringa".equals("First String");

System.out.println("Are strings str and str2, equal?: "+result);

In the above lines of code, the Java String “equals” method has been called on a literal string “First String” in the parameters to the method.  Another string literal with same character sequence is also passed i.e. “First String”. Now, if the Boolean value returned by the aforementioned “equals” method is true, the string “Are strings str and str2, equal?: true” displays in the console.

In the next lines of the code, the literal string ”First Stringa” on which “equals” method is called, and this string has a different character sequence from the one ( “First String”). Now, the “equals” method would return false to the Boolean variable “result”.

To learn essentials of the Java language, watch a tutorial video at Udemy.com.

Java String EqualsIgnoreCase Method

The Java String “equals” method takes the character case of the strings into account when comparing. For instance, in the following example, the “equals” method would return false:

 

String str = new String ("first String");

String str2 = new String ("FIRST STRING");

 

boolean result = str.equals(str2);

System.out.println("Are strings str and str2, equal?: "+result);

In the above example, both str and str2 contain the same character sequences. But in the str object the character sequence is in small letters and in the str2 object, the character sequence is capital.  The equals method would return false, because it is case sensitive in comparison.

You can ignore a string comparison’s character case using a method called equalsIgnoreCase. This method returns true if the character sequence of the two string matches regardless of the character case of the strings being compared. The following example demonstrates this concept:

String str = new String ("first String");

String str2 = new String ("FIRST STRING");

boolean result = str.equalsIgnoreCase(str2);

System.out.println("Are strings str and str2, equal?: "+result);

The Alternative “==” Operator for Java String Comparison

The concept of string comparison using equals operator “==” is shown in the following example:

String str = new String ("First String");

String str2 = new String ("First String");

boolean result = str == str2;

System.out.println("Are strings str and str2, equal?: "+result);

The above example is similar to the one that has already been explained. There is a string str that contains the character sequence “First String”. There is another string str2 that also contains character sequence “First String”. Now, both String type objects str and str2 have the same character sequences. However, they are compared using the equals operator “==”. The returned Boolean variable is false.

There is a very interesting reason for that.  The equals operator “==” doesn’t compare the character sequence of the strings. It compares the reference of the String type objects. If two String objects are referring to same memory location the “==” operator would return true, otherwise false.

In the following example, equals operator “==” would return true:

String str = new String ("First String");

String str2 = new String ("First String");

str = str2;

boolean result = str == str2;

System.out.println("Are strings str and str2, equal?: "+result);

The above code is similar to the last example with only exception:  another line of code “str = str2” has been added after instantiating the str and str2 objects. This would change the reference of str object point towards the reference of str2 object. Now, both string objects have the same reference, so true is returned by the equals operator “==”.

For more interesting Java tutorials, take a course at Udemy.com.