This tutorial will guide you on how to compare chars in Java.
How to Compare chars in Java?
To compare chars in Java, you can use:
-
- compare() method
- equals() method
- Relational operators
Let’s check out each of them one by one.
How to Compare chars by Using compare() Method?
The “compare()” method of the Java “Character” class numerically compares two characters. This method is used to compare the primitive chars and the Character objects. It takes two characters as parameters to compare and returns a numeric value.
Syntax
The syntax of compare() method is given as:
If both chars “x” and “y” are the same, the compare() method will return “0”. If the first char is less than the second char, it will return a negative value. Similarly, the specified method will return a positive value when the first char is greater than the second char.
Example 1: Compare Primitive chars by Using compare() Method
Let’s compare two primitive characters, “f” and “b”, by using the compare() method. These two characters are stored in “ch1” and “ch2”:
char ch2 = 'b';
Now, we will use the “compare()” method to compare these two characters with conditional statements:
System.out.println("character 1 'f' is greater than character 2 'b'");}
else if(Character.compare(ch1, ch2) < 0) {
System.out.println("character 1 'f' is less than character 2 'b'");}
else
System.out.println("Both characters are equal");
The output shows that the “ch1” is numerically greater than “ch2”:
Example 2: Compare Character Objects by Using compare() Method
In a Java program, Character objects can also be compared by using the “compare()” method. First, we will create two Character objects “chr1” and “chr2” with “c” and “v” values respectively:
Character chr2 = 'v';
Now, we call the “compare()” method of the Character class to compare these two character objects using the given conditions:
System.out.println("c is greater than v");}
else if(Character.compare(chr1, chr2) < 0) {
System.out.println("c is less than v");}
else
System.out.println("Both characters are equal");
Output
Let’s head towards the second method!
How to Compare chars by Using equals() Method?
The “equals()” method also belongs to the “Character” class that can be used to compare the Character objects. It simply compares the values and checks whether these characters are equal or not based on their case.
Syntax
The syntax of the equals() method is as follows:
The “equals()” method takes a character object as a parameter. If the values of the “obj1” and “obj2” are equal, the equals() method returns true; otherwise, it returns false.
Example
In this example, we will compare two Character objects, “k” and “K”, by creating two objects, “ch1” and “ch2” of Character class:
Character ch2 = 'K';
If the value of ch1 is equal to ch2, the equals() method will return true; otherwise it will return false.:
System.out.println("Both characters are equal");
else
System.out.println("Both characters are not equal");
As we know, Java is a case-sensitive language, so the equals() method will return false because “k” and “K” are not equal:
How to Compare chars by Using Relational Operators?
There is one more approach for comparing both primitive characters and Character objects, which is using relational operators such as “==”, “<”, and “>”. These operators can be added in the condition based on the requirements.
The following example will compare two Character objects, “ch1” and “ch2,” using the greater than “>” and less than “<” relational operators.
Example
First, we will create two Character objects, “ch1” and “ch2,” with the following values:
Character ch2 = 'm';
Now, we will use relational operators to compare and check if the ch1 object is greater than or less than the other object:
System.out.println("ch1 'n' is less than ch2 'm' ");
}else if (ch1>ch2) {
System.out.println("ch1 is 'n' greater than ch2 'm' ");
}else
System.out.println("Both characters are equal");
Output
We have provided all the necessary instructions to compare chars in Java.
Conclusion
To compare Character objects in Java, you can use compare() method, and to compare both primitive chars and Character objects, utilize the equals() method and relational operators. Java compares primitive values numerically, whereas the Character objects are compared based on their case or values, depending upon the method you utilize. In this tutorial, we thoroughly discussed methods to compare chars in Java.