How to Compare Character in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

This article explains different methods and examples for how to compare characters in java. A variety of built-in Java methods can be used to compare characters in java, such as the compare() and equals() methods. It is possible to compare primitive characters as well as Character objects. The following is a detailed explanation of how to compare characters in java.

Introduction

The Java programming language provides methods for comparing characters, including compare() and equals(). We can compare primitive characters and Character objects. Let's look at these methods in more detail and explore how to compare characters in java. Although we can use the less than and greater than operators, they are only useful for primitive values. The compare(char x, char y) method of the Character class compares two char values numerically in a similar way to the following:

Character.valueoOf(a).compareTo(Character.valueOf(b))

How to Compare Primitive Characters?

a. Using the Character.compare()

In this method, characters are compared using the comparison method of the character classes in this approach. Using this method, you can compare two char values numerically. Afterward, it returns an integer value corresponding to the difference between the ASCII values of the first and second parameters

Example:

In this example we have used the Character.compare() method to compare the characters.

Output:

Code Explanation:

In the above example we have initialized two characters C and D and compared them using Character.compare(). Since the ASCII value of c is less than d we get the output as c is lesser than d

b. Using Relational Operators

In Java, we can compare characters by using relational operators such as <, >, or =. It is possible to compare only primitive characters with this method. In the example below, two characters are compared using relational operators in Java. There are no classes or methods involved in this method, making it the simplest.

Example:

In this example, we have used the relational operator's method to compare the characters.

Output:

Code Explanation:

In the above example, we have initialized two characters E and F and compared them using relational operators. Since the ASCII value of c is less than d we get the output as c is lesser than d

c. Using the Character.hashCode()

This approach compares characters by determining their hashcode, which is their ASCII value. The hashcode() static method returns the hash code of a char value.

Example:

In this example, we have used the Character.hashCode() method to compare the characters.

Output:

Code Explanation:

In the above example, we have initialized two hashcodes @ and #, and compared them using Character.hashCode(). Since the ASCII value of @ is greater than # we get the output as c is lesser than d

How to Compare Character Objects?

a. Using compare()

A compare() method builds a comparison between two class-specific objects (x, y) with parameters. It returns the following values:

  • 0: if (a == b)
  • -1: if (a < b)
  • 1: if (a > b)

Example:

In this example we have used the compare() method to compare the character objects.

Output:

Code Explanation:

In the above example, we have assigned values of a and b and have used the compare() method to check whether they are equal or not. And depending on the result we return 0 (or) 1

b. Using Character.compareTo() method

By calling the compareTo() method on the Java class, an object can be lexicographically compared with the current object. Depending on the value, it will return a positive or negative number. Strings are compared based on their Unicode values.

The first object returns a positive number if it is lexicographically greater than the second object. A negative number is returned if the first object is lexicographically less than the second object, and a zero value is returned if the first object is lexicographically equal to the second object.

Example:

In this example, we have used the Character.compareTo() method to compare the character objects.

Output:

Code Explanation:

In the above example, we have assigned values of a, x, and w and have used Character.compareTo() method to check whether they are equal or not. And depending on the result we return 0, 1, (or) -1

c. Using charValue()

When characters are stored in an object of the Character class, we can use this approach. Using the Character class's charValue() method, we will extract character values from an object. A value that is greater than a, greater than b, or equal to both will be returned when we compare those values with relational operators.

Example

In this example, we have used the charVaue() method to compare the character objects.

Output:

Code Explanation:

In the above example, we have assigned values of a and b and have used Character.charValue() method to check whether they are equal or not. And depending on the result we return true (or) false

d. Using Objects.equals() method

It is possible to apply this approach if the characters are stored in an object of the Character class. The equals method of the Character class compares the current object to the parameter passed as a parameter for equality. In other words, if both objects contain the same character, it returns true, otherwise false.

Example:

In this example, we have used the Object.equals method to compare the character objects.

Output:

Code Explanation:

In the above example we have assigned values of a and b and have used Object.equals() method to check whether they are equal or not. And depending on the result we return true (or) false

Some Examples of How to Compare Characters in Java

a. Check if the string is palindrome or not

We use compare function in this example to compare the characters of the string from both ends.

Output:

Code Explanation:

In the above example, we have used the compare() function to check whether a given string is palindrome or not

b. Check if a character is a vowel or consonant

This example uses the == operator and the || operator to search for vowels and consonants.

Output:

Code Explanation:

In the above example, we have used the relational operators to search for vowels and consonants. Depending upon the input of the user we print it either as a vowel or a consonant

Conclusion

  1. Java's character comparison capabilities are extremely useful when developing real-time applications.
  2. In this article we have examined 7 different methods for comparing characters in Java.
  3. We can select an appropriate comparison approach based on the requirements of an application.
  4. With the help of an example, we learned in detail about these approaches.
  5. In conclusion, this tutorial explains everything you need to know about comparing characters in Java in a clear and concise manner.