Java Data Type Tutorial - Java Character.compareTo(Character anotherCharacter)








Syntax

Character.compareTo(Character anotherCharacter) has the following syntax.

public int compareTo(Character anotherCharacter)

Example

In the following code shows how to use Character.compareTo(Character anotherCharacter) method.

/*from  ww w  .  j av a  2  s  .  c o m*/
public class Main {
    public static void main(String[] args) {
        Character c1 = new Character('a');
        Character c2 = new Character('b');

        int result = c1.compareTo(c2);

        String str1 = "Equal ";
        String str2 = "First character is numerically greater";
        String str3 = "Second character is numerically greater";

        if (result == 0) {
            System.out.println(str1);
        } else if (result > 0) {
            System.out.println(str2);
        } else if (result < 0) {
            System.out.println(str3);
        }
    }
}

The code above generates the following result.