How to Compare Two Strings in Java

On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting (by compareTo() method), and authentication (by equals() method). Following are the three ways to compare two strings in Java.

1) By using == operator

2) By using compareTo() method

3) By using equals() method

1) By Using == Operator

The == operator makes the comparison of references, not values. Let’s understand it with the help of the following program.

FileName: StringComparisonExample.java

Output:


Explanation: Whenever a string is directly assigned to a variable, an object for the assigned string is implicitly created inside the string pool. When the same string is again assigned to another variable, the JVM (Java Virtual Machine) checks whether the object of the string is already exists in the string pool or not. If object is already present, the JVM returns the reference of the created object; otherwise, it creates an object for the assigned string.

In our example, the exact string is assigned twice. Therefore, both the variables str1 and str2 point to the same object, which is created in the string pool. Hence, the if condition returns true, and its print statement is executed.

Now, consider the following program.

FileName: StringComparisonExample1.java

Output:


Explanation: In this example, the str2 variable is holding the reference of the object that is explicitly created. The explicitly created string objects do not reside in the string pool. Therefore, the references held by the variables str1 and str2 are different. Hence, this time the print statement of the else block executes.

2) By Using compareTo() Method

The compareTo() method does the comparison of string lexicographically and renders an integer value that represents whether the first string is either greater than, or less than, or equal to the second string.

Syntax:

anotherStr: represents the string that is being compared with the current string.

An integer value is returned depending on the current string is greater than, or less than, or equal to the anotherStr string.

FileName: StringComparisonExample2.java

Output:

Explanation: The first print statement gives 5 because the ASCII value of ‘R’ is 5 more than ‘M’. The second print statement gives -5 because the ASCII value of ‘M’ is 5 less than ‘R’. The third print statement gives 0 because the contents of str2 and str3 are exactly the same. For the last print statement, the first three letters of the word “Mahi” and “Mahendra” are the same. Therefore, the result is 0 till this point. However, the fourth letter of the word “Mahi”, i.e. the letter i, gets compared with the fourth letter, i.e. the letter e, of the word “Mahendra”. Here, the difference in the ASCII values of the letter is 4, which is being displayed in the output.

3) By Using String.equals() Method

The equals() method compares the content of the strings and returns true value when there is a complete match; otherwise, false value. Note that it overrides the equals() method of the Object class.

Syntax:

Output:

Explanation: The first print statement gives 5 because the ASCII value of ‘R’ is 5 more than ‘M’. The second print statement gives -5 because the ASCII value of ‘M’ is 5 less than ‘R’. The third print statement gives 0 because the contents of str2 and str3 are precisely the same.

Pin It on Pinterest

Share This