How to Compare Characters in Java

In this tutorial, I will be sharing how to compare characters in java. Characters can be a char primitive data type or Character wrapper class. Let's dive deep into the topic: 

Read Also:  Find the first non-repeated character in the String

1. Comparing char primitives

1.   Using <, >, ==  operators

Based on the Unicode table, the char primitive data type also has the associated integer value. Using ==, <,  > operators you should be able to compare two characters just like you compare two integers.

Note:  Comparing char primitive values using < , > or == operators returns a boolean value.

Example:

public class JavaHungry {
    public static void main(String args[]) {
      char ch1 = 'A';
      char ch2 = 'B';
      char ch3 = 'A';
      System.out.println(ch1==ch2);
      System.out.println(ch1<ch2);
      System.out.println(ch3==ch1);
    }
}

Output:
false
true
true


2. Using Character.compare(char x, char y)

We can compare the char values numerically by using compare(char x, char y) method of Character class.

Note:  Comparing char primitive values using Character.compare(char x, char y) method returns an integer value.


According to Java docs,
The compare(char a, char b) method of Character class returns the following values:
0, if (a == b)
value less than 0, if a < b
value greater than 0, if a > b

Example:

public class JavaHungry {
    public static void main(String args[]) {
      char ch1 = 'X';
      char ch2 = 'Y';
      char ch3 = 'X';
      System.out.println(Character.compare(ch1,ch2));
      System.out.println(Character.compare(ch2,ch3));
      System.out.println(Character.compare(ch1,ch3));
    }
}

Output:
-1
1
0

2. Comparing Character objects

You can compare Character class objects:

1. Using Character.compare(char x, char y)

Using Character class constructor, we can convert the char primitive value to the Character object.

public class JavaHungry {
    public static void main(String args[]) {
      Character ch1 = new Character('X');
      Character ch2 = new Character('Y');
      Character ch3 = new Character('X');
      System.out.println(Character.compare(ch2,ch3));
      System.out.println(Character.compare(ch1,ch3)); 
      System.out.println(Character.compare(ch1,ch2)); 
 }
}

Output:
1
0
-1

2. Using equals() method

Using equals() method also we can compare the Character class objects.

Note: Comparing Character class objects using equals() method returns a boolean value.


public class JavaHungry {
    public static void main(String args[]) {
      Character ch1 = new Character('X');
      Character ch2 = new Character('Y');
      Character ch3 = new Character('X');
      System.out.println(ch1.equals(ch2));
      System.out.println(ch2.equals(ch3));
      System.out.println(ch1.equals(ch3));
    }
}

Output:
false
false
true

That's all for today, please mention in comments in case you have any questions related to how to compare characters in java.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry