Compare two Strings in Java

In this article, you will learn how to Compare two strings in Java. Strings are widely used in Java programming. It is a sequence of characters. In Java, String objects are immutable which means a constant and cannot be changed once created.

This tutorial shows you total 6 ways to compare two Strings in Java:

  1. Using “==” operator
  2. Using String.equals()
  3. Using String.equalsIgnoreCase()
  4. Using Objects.equals()
  5. Using String.compareTo()
  6. Using a user-defined (custom) method

String Compare using == Operator

The == operator is a type of relational operator in Java. It is used to check for relations of equality and returns a boolean result after the comparison. Below is the java code example to compare two strings using the == operator,

package com.javacodepoint.string;

public class CompareTwoStrings {

	public static void main(String[] args) {

		// Declaring & Initializing multiple Strings
		String s1 = new String("Javacodepoint");
		String s2 = new String("Javacodepoint");
		String s3 = "Javacodepoint";
		String s4 = "Java";
		String s5 = "Java";

		// Comparing two Strings
		System.out.println("Comparing " + s1 + " and " + s2 + " => " + (s1 == s2));// false

		System.out.println("Comparing " + s2 + " and " + s3 + " => " + (s2 == s3));// false

		System.out.println("Comparing " + s3 + " and " + s4 + " => " + (s3 == s4));// false

		System.out.println("Comparing " + s4 + " and " + s5 + " => " + (s4 == s5));// true

		System.out.println("Comparing " + s1 + " and " + s5 + " => " + (s1 == s5));// false

	}

}

OUTPUT:

Comparing Javacodepoint and Javacodepoint => false
Comparing Javacodepoint and Javacodepoint => false
Comparing Javacodepoint and Java => false
Comparing Java and Java => true
Comparing Javacodepoint and Java => false

Output Explanation:
Here, s1 and s2 hold the same string content (“Javacodepoint”) but refer to different objects. When we use the == operator for the s1 and s2 comparison then the result is false as both have different addresses in memory.

NOTE:

The == operator is not a recommanded approach to compare two strings in java because it checks only both objects point to the same memory location (address) or not. It’s not checking the content of the objects.

Compare two Strings using String.equals()

In this example, we use the equals() method of the String class. This method compares two strings and returns true if the strings are equal, and false if not.

package com.javacodepoint.string;

public class CompareTwoStrings2 {

	public static void main(String[] args) {

		// Declaring & Initializing multiple Strings
		String s1 = new String("Javacodepoint");
		String s2 = new String("JavaCodePoint");
		String s3 = new String("Java");
		String s4 = new String("Java");
		String s5 = new String("JAVA");
		String s6 = new String("Example");

		// Comparing two Strings
		System.out.println("Comparing " + s1 + " and " + s2 + " => " + s1.equals(s2));// false

		System.out.println("Comparing " + s3 + " and " + s4 + " => " + s3.equals(s4));// true

		System.out.println("Comparing " + s4 + " and " + s5 + " => " + s4.equals(s5));// false

		System.out.println("Comparing " + s5 + " and " + s6 + " => " + s5.equals(s6));// false

		System.out.println("Comparing " + s1 + " and " + s6 + " => " + s1.equals(s6));// false

	}

}

OUTPUT:

Comparing Javacodepoint and JavaCodePoint => false
Comparing Java and Java => true
Comparing Java and JAVA => false
Comparing JAVA and Example => false
Comparing Javacodepoint and Example => false

Compare two Strings using String.equalsIgnoreCase()

In this example, we use the equalsIgnoreCase() method. This method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not.

package com.javacodepoint.string;

public class CompareTwoStrings3 {

	public static void main(String[] args) {

		// Declaring & Initializing multiple Strings
		String s1 = new String("Javacodepoint");
		String s2 = new String("JavaCodePoint");
		String s3 = new String("JAVACODEPOINT");
		String s4 = new String("Java");
		String s5 = new String("JAVA");

		// Comparing two Strings
		System.out.println("Comparing " + s1 + " and " + s2 + " => " + s1.equalsIgnoreCase(s2));// true

		System.out.println("Comparing " + s1 + " and " + s3 + " => " + s1.equalsIgnoreCase(s3));// true

		System.out.println("Comparing " + s3 + " and " + s4 + " => " + s3.equalsIgnoreCase(s4));// false

		System.out.println("Comparing " + s4 + " and " + s5 + " => " + s4.equalsIgnoreCase(s5));// true

	}

}

OUTPUT:

Comparing Javacodepoint and JavaCodePoint => true
Comparing Javacodepoint and JAVACODEPOINT => true
Comparing JAVACODEPOINT and Java => false
Comparing Java and JAVA => true

Compare two Strings Objects.equals()

In this example, we use the equals() method of the Objects class. This method compares two strings and returns true if the strings are equal, and false if not.

package com.javacodepoint.string;

import java.util.Objects;

public class CompareTwoStrings4 {

	public static void main(String[] args) {

		// Declaring & Initializing multiple Strings
		String s1 = new String("Javacodepoint");
		String s2 = new String("Javacodepoint");
		String s3 = null;
		String s4 = new String("Java");
		String s5 = new String("Java");

		// Comparing two Strings
		System.out.println("Comparing " + s1 + " and " + s2 + " => " + Objects.equals(s1, s2));// true

		System.out.println("Comparing " + s1 + " and " + s3 + " => " + Objects.equals(s1, s3));// false

		System.out.println("Comparing " + s3 + " and " + s4 + " => " + Objects.equals(s3, s4));// false

		System.out.println("Comparing " + s4 + " and " + s5 + " => " + Objects.equals(s4, s5));// true

	}

}

OUTPUT:

Comparing Javacodepoint and Javacodepoint => true
Comparing Javacodepoint and null => false
Comparing null and Java => false
Comparing Java and Java => true

Compare two Strings using String.compareTo()

The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. This method returns 0 if both strings are equal, a negative integer if the string is less than the other string, and a positive integer if the string is greater than the other string.

package com.javacodepoint.string;

public class CompareTwoStrings5 {

	public static void main(String[] args) {

		// Declaring & Initializing multiple Strings
		String s1 = new String("Javacodepoint");
		String s2 = new String("Javacodepoint");
		String s3 = new String("JAVA");
		String s4 = new String("Java");
		String s5 = new String("HELLO");

		// Comparing two Strings
		System.out.println("Comparing " + s1 + " and " + s2 + " => " + s1.compareTo(s2));

		System.out.println("Comparing " + s1 + " and " + s3 + " => " + s1.compareTo(s3));

		System.out.println("Comparing " + s3 + " and " + s4 + " => " + s3.compareTo(s4));

		System.out.println("Comparing " + s4 + " and " + s5 + " => " + s4.compareTo(s5));

	}

}

OUTPUT:

Comparing Javacodepoint and Javacodepoint => 0
Comparing Javacodepoint and JAVA => 32
Comparing JAVA and Java => -32
Comparing Java and HELLO => 2

Compare two Strings using the user-defined (Custom) method

In this example, we have created a user-defined (custom) method for comparing two strings.

Custom Approach to Compare two Strings in Java:

  1. Let’s take two strings s1 and s2.
  2. Find the length of both strings using the length() method and named them as s1Length, and s2Length.
  3. Find the minimum length (minLength) among both string lengths.
  4. Iterate a loop 0 to <minLength.
  5. Inside the loop, check the individual characters s1_ch, and s2_ch. if not equal then return the subtract of it.
  6. After the loop is complete, check for s1Length, and s2Length are not equals then return the subtract of it else return 0.

Let’s see the java program for it below:

package com.javacodepoint.string;

public class CompareTwoStrings6 {

	// Custom method to compare two Strings
	public static int compareTwoStrings(String s1, String s2) {

		// Get length of both string
		int s1Length = s1.length();
		int s2Length = s2.length();

		// Find minimum length
		int minLength = Math.min(s1Length, s2Length);

		for (int i = 0; i < minLength; i++) {
			int s1_ch = (int) s1.charAt(i);
			int s2_ch = (int) s2.charAt(i);

			if (s1_ch != s2_ch) {
				return s1_ch - s2_ch;
			}
		}

		if (s1Length != s2Length) {
			return s1Length - s2Length;
		} else {
			// both the strings are equal
			return 0;
		}
	}

	// Main method
	public static void main(String[] args) {

		// Declaring & Initializing multiple Strings
		String s1 = new String("Javacodepoint");
		String s2 = new String("JavaCodePoint");
		String s3 = new String("Java");
		String s4 = new String("Java");
		String s5 = new String("JAVA");
		String s6 = new String("Hello World");

		// Comparing two Strings
		System.out.println("Comparing " + s1 + " and " + s2 + " => " + compareTwoStrings(s1, s2));

		System.out.println("Comparing " + s3 + " and " + s4 + " => " + compareTwoStrings(s3, s4));

		System.out.println("Comparing " + s4 + " and " + s5 + " => " + compareTwoStrings(s4, s5));

		System.out.println("Comparing " + s5 + " and " + s6 + " => " + compareTwoStrings(s5, s6));

		System.out.println("Comparing " + s1 + " and " + s6 + " => " + compareTwoStrings(s1, s6));

	}

}

OUTPUT:

Comparing Javacodepoint and JavaCodePoint => 32
Comparing Java and Java => 0
Comparing Java and JAVA => 32
Comparing JAVA and Hello World => 2
Comparing Javacodepoint and Hello World => 2

Conclusion

In this tutorial, you have seen the comparison of two Strings with various code examples in Java programming. You can take reference for various String methods API here: Methods of String class.

See also:
Java Program to Count the Number of Vowels in a String.
Java Program to Swap two strings without using the third variable.

FAQs

What is the difference between == equals () and compareTo () method?

The == operator is a type of relational operator in Java used to check for relations of equality. The String class equals() method compares the original content of the string. It compares values of string for equality. The equals() tells the equality of two strings whereas the compareTo() method tells how strings are compared lexicographically.

Can you use == to compare string in Java?

To compare these strings in Java, we need to use the equals() method of the string. You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not.

Is compareTo a boolean?

The compareTo() method of a Boolean class is a built-in method in Java that is used to compare the given Boolean instance with the current instance.

Share with friends

Leave a Comment

Your email address will not be published. Required fields are marked *