How to compare characters in Java

In this article, we are going to compare characters in Java.
Java provides some built-in methods such compare() and equals() to compare the character objects. Although, we can use less than or greater than operators but they work well with primitive values only.
[toc]
Let’s take some examples to compare characters in Java.

Compare primitive chars

You can compare primitive chars either using Character.compare() method or <, > or = relational operators.

Using compare()

The compare() method of Characters class returns a numeric value positive, negative or zero.
See the example below.

class Main {

    public static void main(String[] args){

        char a = 'a';
        char b = 'b';
        if(Character.compare(a, b) > 0) {
            System.out.println("a is greater");
        }else if(Character.compare(a, b) < 0) {
            System.out.println("a is less than b");
        }else 
            System.out.println("Both are equal");
    }
}

Output

a is less than b

[su_note note_color="#f5f7f7"]
[su_list icon="icon: check"]
Recommended read

[/su_list]
[/su_note]

Using relation operators

We can use relational operators like less than or greater than to compare two characters in Java. It is simplest approach and does not involve any class or method.

class Main {

    public static void main(String[] args){

        char a = 'a';
        char b = 'b';
        if(a > b) {
            System.out.println("a is greater");
        }else if(a < b) {
            System.out.println("a is less than b");
        }else 
            System.out.println("Both are equal");
    }
}

Output

a is less than b

Compare Character objects

You can compare primitive chars either using Character.compare() method or equals() method.

Using compare()

You can use compare() method with Character objects as well. The compare() method of Characters class returns a numeric value positive, negative or zero.
See the example below.

class Main {

    public static void main(String[] args){

        Character ch1 = 'x';
        Character ch2 = 'y';
        if(Character.compare(ch1, ch2) > 0) {
            System.out.println("x is greater");
        }else if(Character.compare(ch1, ch2) < 0) {
            System.out.println("x is less than y");
        }else 
            System.out.println("Both are equal");
    }
}

Output

x is less than y

Using Equals()

The equals() method is used to check whether two char objects are equal or not. It returns true if both are equal else returns false.

class Main {

    public static void main(String[] args){

        Character a = 'a';
        Character b = 'b';

        if(a.equals(b)) {
            System.out.println("a is equals b");
        }else 
            System.out.println("a is not equal to b");
    }
}

Output

a is not equal to b

That's all about How to compare characters in Java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.