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.

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.

Was this post helpful?

Related Posts

  • Get unicode value of character in java
    07 February

    Get Unicode Value of Character in Java

    Table of ContentsGet Unicode Value of Character in JavaGet Unicode Character Code in JavaWas this post helpful? In this post, we will see how to get unicode value of character in java. Get Unicode Value of Character in Java You can simply use below code to get unicode value of character in java. [crayon-62b86c9912a15159252523/] Here […]

  • 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

    Table of ContentsUsing \n or \r\nUsing Platform independent line breaks (Recommended)Was this post helpful? 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 […]

  • 09 October

    Java program to print vowels in a String

    Table of ContentsPrint Vowels in a StringCount number of Vowels in the StringWas this post helpful? 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. […]

  • 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

    Table of ContentsMethod signatureParametersReturn typeWas this post helpful? You can use Character class’s toLowerCase method to convert char to lowercase in java. Method signature [crayon-62b86c9915cbe179155763/] Parameters ch is primitive character type. Return type return type is char. If char is already lowercase then it will return same. [crayon-62b86c9915cc5474433809/] When you run above program, you will […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.