Comparison in Java

In Java there exists ways to compare items. I recently came across the question of what these ways are and how they work. For this purpose, this blog will be focused on describing the three techniques of comparison in regards to comparing Strings and by the end, we should both have a better understanding of how they’re different and when you would choose one method over the others.

Using the == Operator

The == operator will only compare references, not the values themselves. Here is an example.

public class Comparison {
	
	public static void main(String[] args) {
		String sA = "Hello";
		String sB = "Hello";
		String sC = new String("Hello");
		
		System.out.println(sA == sB);
		System.out.println(sA == sC);
		
	}
}

When executed, the console shows as such: this is because the first print statement is comparing if sA and sB share the same reference which they do, so it turns out true. The second print statement is comparing if sA and sC share the same reference and they do not, so it turns out false.

Using the Equals() Method

The Equals() method compares values for equality, making it great for authentication. It is automatically defined for every class you create because it is defined in the Object class of Java. Let’s see it in action.

public class Comparison {
	
	public static void main(String[] args) {
		String sA = "Hello";
		String sB = "Hello";
		String sC = new String("Hello");
		String sD = "Goodbye";
		
		System.out.println(sA.equals(sB));
		System.out.println(sB.equals(sC));
		System.out.println(sC.equals(sD));
		System.out.println(sA.equals(sD));
	}
}

This is the output; note that regardless of reference, as long as the values are equal it will turn out true.

Using the CompareTo() Method

Where the previous two options returned a boolean, the compareTo() method that is included in the Comparable interface differs in the fact that it will return an integer value to indicate if the first String is less than, equal to or greater than the second String. It determines which value to return based on the Unicode value of each character between the two strings. If the two Strings are equal, it will return 0, if the first is less than the second it will return a negative number and if the first is greater than the second, it will return a positive number. You would want to use the compareTo() method when you need to sort Strings. Here’s an example.

public class Comparison {

	public static void main(String[] args) {
		String sA = "Hello";
		String sB = "Hello";
		String sC = "Goodbye";
		String sD = "Salutations";
		
		System.out.println(sA.compareTo(sB));
		System.out.println(sA.compareTo(sC));
		System.out.println(sC.compareTo(sA));
		System.out.println(sD.compareTo(sB));
	}

}

Since sA is the same value as sB, “Hello”, it returns as 0. Next sA is compared to sC and since “Hello” is greater than “Goodbye” we get 1. On the flipside, when we compare sC to sA, we get -1 since “Goodbye” is less than “Hello.” Finally we compare sD to sB and we get 11 since Salutations is 11 less than Hello, alphabetically.

In conclusion, you’d want to use the == operator when you want to compare if Strings have the same reference, the equals() method when you want to check that String values are the same such as for authentication and compareTo() method would fit best when you want to sort your objects.

Source: https://www.javatpoint.com/string-comparison-in-java
The Full Code on GitHub: https://github.com/salkiduckyJUMP/String-Comparison-Demos

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.