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.

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.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Using compare()

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

Output

a is less than b

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.

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.

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.

Output

a is not equal to b

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

Related Posts

  • Convert chartacter to ascii in java
    04 May

    Convert Character to ASCII Numeric Value in Java

    Learn about how to convert Character to ASCII Numeric Value in Java.

  • 17 March

    New line character in java

    In this post, we will see about new line character in java and how to add new line character to a String in different operating systems. Operating systems have different characters to denote the end of the line. Linux and new mac: In Linux, the end line is denoted by \n, also known as line […]

  • 09 October

    Java program to print vowels in a String

    In this post, we will see how to print and count vowels in a string. Print Vowels in a String If any character in String satisfy below condition then it is vowel and we will add it to Hashset. character==’a’ || character==’A’ || character==’e’ || character==’E’ || character==’i’ || character==’I’ || character==’o’ || character==’O’ || […]

  • 06 October

    Java remove last character from string

    Learn about how to remove last character from String in java using different ways.

  • 06 October

    Convert char to lowercase java

    You can use Character class’s toLowerCase method to convert char to lowercase in java. Method signature [crayon-609211b6e051d828159352/] Parameters ch is primitive character type. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Return type return type is char. If char […]

  • 06 October

    Convert char to uppercase java

    You can use Charater class’s touppercase method to convert char to uppercase in java. Method signature [crayon-609211b6e06cd519523099/] Parameters ch is primitive character type. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Return type return type is char. If char […]

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.