java.lang.Character compareTo(Character anotherCharacter)

Description

The compareTo(Character anotherCharacter) java method compares two Character objects numerically.Don’t get confused on the usage of this method to that of the compare() method. The compareTo() method is used to compare two Character object while the compare() method is used primarily to compare two character primitive. But since autoboxing and unboxing features are already available in java starting at java 1.5, you can easily used this two method regardless if its primitive or an object.

Important Notes:

  • Specified by compareTo in interface Comparable<Character>

Method Syntax

public int compareTo(Character anotherCharacter)

Method Argument

Data Type Parameter Description
Character anotherCharacter the Character to be compared.

Method Returns

The compare(char x, char y) method of Character class returns the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y.

Compatibility

Requires Java 1.2 and up

Java Character compareTo(Character anotherCharacter) Example

Below is a simple java example on the usage of compareTo(Character anotherCharacter) method of Character class.

Sample Output

Below is the sample output when you run the above example.

java Character compareTo(Character anotherCharacter) example output