Java Program to Compare Strings



In this article, we will compare two Strings in Java using the compareTo() method, equals() method, or == operator. Each approach serves a specific purpose in comparing strings, whether it's lexicographical comparison, checking for content equality, or comparing object references.

Using the compareTo() method

The compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

  • The result is a negative integer if this String object lexicographically precedes the argument string.
  • The result is a positive integer if this String object lexicographically follows the argument string.
  • The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would return true.

Variations of string comparison using compareTo() method

The following are the different string comparisons using the compareTo() method ?

  • str.compareTo(another string) compares the strings considering their Unicode values.
  • str.compareToIgnoreCase(another String) compares the strings ignoring case sensitivity.
  • str.compareTo(objStr.toString()) compares the string str with the string value of objStr.

Example

public class StringCompareEmp {
	public static void main(String args[]) {
		String str = "Hello World";
		String anotherString = "hello world";
		Object objStr = str;
		System.out.println( str.compareTo(anotherString) );
		System.out.println( str.compareToIgnoreCase(anotherString) );
		System.out.println( str.compareTo(objStr.toString()));
	}
}

Output

-32
0
0

Using the equals() method

The equals() method of the String class compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

  • Returns true if both strings contain the same characters in the same order.
  • Returns false if the character sequences differ, even by one character.
  • It is case-sensitive.

Usage of equals() method in string comparison

The following are the different ways of the equals() method in string comparison ?

  • s1.equals(s2) returns true because both strings have the same content.
  • s2.equals(s3) returns false because the content of the strings is different (case-sensitive).

Example

public class StringCompareEqual {
	public static void main(String []args) {
		String s1 = "tutorialspoint";
		String s2 = "tutorialspoint";
		String s3 = new String ("Tutorials Point");
		System.out.println(s1.equals(s2));
		System.out.println(s2.equals(s3));
	}
}

Output

true
false

Using the == operator

The == operator compares the references of two string objects to check if they point to the same memory location.

  • Returns true if both references point to the same object in memory.
  • Returns false if the references point to different objects, even if they have the same content.

Usage of == operator in string comparison

The following are the different ways of the == operator method in string comparison ?

  • s1 == s2 returns true because both variables point to the same memory location.
  • s2 == s3 returns false because s3 points to a different memory location despite having the same content.

Example

public class StringCompareequl {
	public static void main(String []args) {
		String s1 = "tutorialspoint";
		String s2 = "tutorialspoint";
		String s3 = new String ("Tutorials Point");
		System.out.println(s1 == s2);
		System.out.println(s2 == s3);
	}
}

Output

true
false

Updated on: 2025-01-23T22:54:05+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements