What is CharUtils.compare in Java?

compare is a static method of the CharUtils class that is used to compare two characters numerically. The method returns an integer indicating the difference between the numerical values of the characters.

  • The method returns zero if both the characters are the same.

  • The method returns a value greater than zero if the numerical value of the first character is greater than the second character.

  • The method returns a value less than zero if the numerical value of the first character is less than the second character.

How to import CharUtils

CharUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

We can import the CharUtils class as follows.

import org.apache.commons.lang3.CharUtils;

Syntax


public static int compare(char x, char y)

Parameters

  • x: the first character to compare.
  • y: the second character to compare.

Return value

The function returns the difference of the numerical values of the two characters.

Code

import org.apache.commons.lang3.CharUtils;
public class Main {
public static void main(String[] args) {
char c1 = 'A';
char c2 = 'X';
System.out.printf("The output of CharUtils.compare() for the characters '%s' and '%s' is %s", c1, c2, CharUtils.compare(c1, c2));
System.out.println();
c1 = 'Z';
c2 = 'B';
System.out.printf("The output of CharUtils.compare() for the characters '%s' and '%s' is %s", c1, c2, CharUtils.compare(c1, c2));
System.out.println();
c1 = 'a';
c2 = 'a';
System.out.printf("The output of CharUtils.compare() for the characters '%s' and '%s' is %s", c1, c2, CharUtils.compare(c1, c2));
System.out.println();
}
}

Expected output


The output of CharUtils.compare() for the characters 'A' and 'X' is -23
The output of CharUtils.compare() for the characters 'Z' and 'B' is 24
The output of CharUtils.compare() for the characters 'a' and 'a' is 0
The output of CharUtils.compare() for the characters 'È' and '#' is 165

Explanation

  • c1 = 'A'

  • c2 = 'X'

    The integer valuesASCII values in this case of c1 and c2 are 65 and 88 respectively. The difference value i.e (c1 - c2) = (65 - 88) = -23 is returned by the method.

  • c1 = 'Z'

  • c2 = 'B'

    The integer valuesASCII values in this case of c1 and c2 are 90 and 66 respectively. The difference value i.e., (c1 - c2) = (90 - 66) = 24 is returned by the method.

  • c1 = 'a'

  • c2 = 'a'

    The integer valuesASCII values in this case of c1 and c2 are 97 and 97 respectively. The difference value i.e. (c1 - c2) = (97 - 97) = 0 is returned by the method.

  • c1 = 'È' (Unicode character)

  • c2 = '#'

    The integer values of c1 and c2 are 200 and 35 respectively. The difference value i.e (c1 - c2) = (200 - 35) = 165 is returned by the method.

Free Resources