Different methods to compare char in Java
In Java, char is a primitive data type. Whereas, the Character is a wrapper class that is used to wrap a value of the primitive type char in an object. Moreover, an object of Character class contains a single field whose type is char.
There are several ways to compare characters in java as listed below.
- Using Relational Operators
- Using
Character.compare()
- Using
Character.hashCode()
- Using
compareTo()
method - Using
equals()
method - Using
charValue()
method
Using Relational Operators
This is the simplest approach to compare characters in java. Here, we are using relational operators supported in Python to compare the characters in the same way as any numeric value.
Example :
// Program to compare characters
public class Main
{
public static void main(String[] args)
{
// Initializing the variables
char a='A';
char b='B';
// Comparing using relational operators
System.out.println("The Character "+a+" is less than the character "+b+" : "+(a<b)); a='b'; System.out.println("The Character "+a+" is greater than the character "+b+" : "+(a>b));
a='B';
System.out.println("The Character "+a+" is equal to the character "+b+" : "+(a==b));
}
}
Output
The Character A is less than the character B : true
The Character A is greater than the character a : false
The Character a is equal to the character a : true
Method-1: Using Character.compare()
In this approach, we are comparing character using compare method of character class. This is a static method that compares two char values numerically. Thereafter, it returns an integer value giving the difference of ASCII value of first parameter by second parameter.
For example,
If we compare the character A and B. Then the result will be ASCII(A)-ASCII(B) = 65-66 = -1
.
The syntax of compare function is as shown below. It returns an integer value.
Character.compare(char1, char2)
Example :
// Program to compare characters
public class Main
{
public static void main(String[] args)
{
// Initializing the variables
char a='A';
char b='B';
// Comparing using Character.compare
System.out.println("The ASCII value of "+a+" - ASCII value of "+b+" = "+Character.compare(a,b));
a='b';
System.out.println("The ASCII value of "+a+" - ASCII value of "+b+" = "+Character.compare(a,b));
}
}
Output
The ASCII value of A - ASCII value of B = -1
The ASCII value of b - ASCII value of B = 32
Method-2: Using Character.hashCode()
In this approach, we will compare characters by finding their hashcode which is an ASCII value of that character. The hashcode() method is a static method that returns a hash code for a char type value.
The syntax of hashCode method is as shown below. It returns an equivalent integer value.
Character.hashCode(char)
Example :
// Program to compare characters
public class Main
{
public static void main(String[] args)
{
// Initializing the variables
char a='@';
char b='#';
// Printing ASCII value of characters
System.out.println(a+" = "+Character.hashCode(a));
System.out.println(b+" = "+Character.hashCode(b));
// Comparing
System.out.println("Comparing "+a+" == "+b+" : "+(Character.hashCode(a) == Character.hashCode(b)));
System.out.println("Comparing "+a+" < "+b+" : "+(Character.hashCode(a) < Character.hashCode(b))); System.out.println("Comparing "+a+" > "+b+" : "+(Character.hashCode(a) > Character.hashCode(b)));
}
}
Output
@ = 64
# = 35
Comparing @ == # : false
Comparing @ < # : false Comparing @ > # : true
Method-3: Using compareTo()
We can use this approach, if the characters stored in an object of Character class. Here, we will compare characters using compareTo
method of Character class. This method returns the result in the same way as compare()
method by computing the difference of ASCII value. The only difference between the compare()
and the compareTo()
method is that compare()
method works on primitive type char. Whereas, the compareTo()
method works on object of Character class.
The syntax of compareTo
method is as shown below. It returns an integer value.
obj1.compare(obj2)
Example :
// Program to compare characters
public class Main
{
public static void main(String[] args)
{
// Creating object of Character class
Character a= new Character('A');
Character b = new Character('a');
// Comparing using compareTo
System.out.println("Object Comparison");
System.out.println("Comparing "+Character.valueOf(a)+" with "+Character.valueOf(b)+" Result is "+a.compareTo(b));
}
}
Output
Object Comparison
Comparing A with a Result is -32
Method-4: Using equals()
We can use this approach, if the characters are stored in an object of Character class. Here, we will compare characters using equals method of Character class. This method compares current object with the one passed as an parameter for an equality. So, if the character stored in both the object are equal, it will return true, false otherwise.
The syntax of equals()
method is as shown below. It returns an integer value.
obj1.equals(obj2)
Example :
// Program to compare characters
public class Main
{
public static void main(String[] args)
{
// Object Creation
Character a= new Character('A');
Character b = new Character('a');
// Comparing characters
System.out.println("Object Comparison");
System.out.println("Comparing "+Character.valueOf(a)+" with "+Character.valueOf(b)+" Result is "+a.equals(b));
}
}
Output
Object Comparison
Comparing A with a Result is false
Method-5: Using charValue()
We can use this approach, if the characters are stored in an object of Character class. Here, we will extract character from an object using the charValue()
method of Character class. Thereafter, we will compare those values using relational operators and result will be a boolean value true or false.
The syntax of charValue
function is as shown below. It returns an integer value.
obj.charValue()
Example :
// Program to compare characters
public class Main
{
public static void main(String[] args)
{
// Object creation
Character a = new Character('@');
Character b = new Character('#');
// Comparing characters
System.out.println("Comparing "+a+" == "+b+" : "+(a.charValue() == b.charValue()));
System.out.println("Comparing "+a+" < "+b+" : "+(a.charValue() < b.charValue())); System.out.println("Comparing "+a+" > "+b+" : "+(a.charValue() > b.charValue()));
}
}
Output
Comparing @ == # : false
Comparing @ < # : false Comparing @ > # : true
Compare Characters Examples
Example : Check if the string is palindrome or not
In this example, we are using compare function to compare the characters of the string from both the end.
public class Main {
public static void main(String[] args)
{
String str = "madam";
char a,b;
int i = 0, j = str.length() - 1;
while (i <= j) { a=str.charAt(i); b=str.charAt(j); System.out.println("Comparing "+a+" and "+b); if (Character.compare(a,b)!=0) break; i++; j--; } if(i>j)
System.out.println("This is a Palindrome String");
}
}
Output
Comparing m and m
Comparing a and a
Comparing d and d
This is a Palindrome String
Example : Check if a character is vowel or consonant
In this example, we are using == relational operator to check for vowels and consonants.
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a character");
char y=sc.next().charAt(0);
if (y == 'a' || y == 'e' || y == 'i' || y == 'o' || y == 'u')
System.out.println("It is a Vowel.");
else
System.out.println("It is a Consonant.");}
}
Output
Enter a character
s
It is a Consonant.
Summary
The knowledge of comparing characters in Java is very useful while working on a real time applications. In this tutorial, we covered six different approaches to compare the characters in java Java. As per the requirement of an application, we can choose an appropriate approach for comparison. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on compare characters in Java.
References