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.

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

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

  • 09 October

    Java program to print vowels in a String

    Table of ContentsPrint Vowels in a StringCount number of Vowels in the 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’ […]

  • 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 type You can use Character class’s toLowerCase method to convert char to lowercase in java. Method signature [crayon-60ef25112162c933262486/] Parameters ch is primitive character type. Return type return type is char. If char is already lowercase then it will return same. [crayon-60ef251121632494047219/] When you run above program, you will get below output: […]

  • 06 October

    Convert char to uppercase java

    Table of ContentsMethod signatureParametersReturn type You can use Charater class’s touppercase method to convert char to uppercase in java. Method signature [crayon-60ef251121f64230230550/] Parameters ch is primitive character type. Return type return type is char. If char is already uppercase then it will return same. [crayon-60ef251121f6a400566290/] When you run above program, you will get below output: […]

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.