The Char equals Method in Java
-
Check Equal Char Using the
==
Equal Operator in Java -
Check Equal Char Using the
equals()
Method in Java -
Check Equal Char Using the
compare()
Method in Java

This tutorial introduces how to check whether two chars are equal or not in Java.
In Java, we can compare two characters either by using the equals(==
) operator or the equals()
method of the Character
class. If you are working with primitive char values, you can simply use the ==
equal operator but use the characters class instances, use the equals()
method.
In this article, we will learn the use of both equals methods with the help of examples. Let’s get started.
Check Equal Char Using the ==
Equal Operator in Java
Java uses the ==
equal operator to check whether two values are equal or not. We can use this operator to check two characters are equal or not.
In this example, we created three chars and compared them using the == equals operator. This operator returns true if both the chars are equal, false otherwise.
public class SimpleTesting{
public static void main(String[] args){
char ch1 = 'J';
char ch2 = 'K';
char ch3 = 'J';
System.out.println(ch1 == ch2);
System.out.println(ch2 == ch3);
System.out.println(ch1 == ch3);
}
}
Output:
false
false
true
Check Equal Char Using the equals()
Method in Java
If you are working with the Character
class and want to compare two char values, then use equals()
method that belongs to the Object
class and returns true if the object is equal, false otherwise. See the example below.
public class SimpleTesting{
public static void main(String[] args){
Character ch1 = 'J';
Character ch2 = 'K';
Character ch3 = 'J';
System.out.println(ch1.equals(ch2));
System.out.println(ch2.equals(ch3));
System.out.println(ch1.equals(ch3));
}
}
Output:
false
false
true
Check Equal Char Using the compare()
Method in Java
This is another solution that can be used to check the equality of two chars. The compare()
method belongs to the String class and returns 0 if both the values are equal.
Here, we used this method with the == equals operator to verify if it returns 0 or not. If it returns 0, then both values are equal. See the example below.
public class SimpleTesting{
public static void main(String[] args){
Character ch1 = 'J';
Character ch2 = 'K';
Character ch3 = 'J';
System.out.println(Character.compare(ch1,ch2)==0);
System.out.println(Character.compare(ch2,ch3)==0);
System.out.println(Character.compare(ch1,ch3)==0);
}
}
Output:
false
false
true
While checking the equality of two objects, always check the values. Java does not consider lowercase and uppercase equal. We think that both the values are the same, but Java works on Unicode values, and both the variables hold different Unicode. That is why Java returns false to the console. See the code example and understand Java deals differently for lowercase and uppercase characters.
public class SimpleTesting{
public static void main(String[] args){
Character ch1 = 'J';
Character ch2 = 'j';
System.out.println(ch1 == ch2);
}
}
Output:
false