Character compareTo() in Java

compareTo(): This method is available in java.lang.Character class of Java.

Syntax:

int java.lang.Character.compareTo(Character anotherCharacter)

This method takes one argument of type Character as its parameter. This method compares two Character objects numerically.

Parameters: One parameter is required for this method.

anotherCharacter: the Character to be compared.

Returns:

1. The value 0 if the argument Character is equal to this Character.

2. A value less than 0 if this Character is numerically less than the Character argument.

3. A value greater than 0 if this Character is numerically greater than the Character argument (unsigned comparison).

Approach

Java

public class CharactercompareTo {
    public static void main(String[] args) {

        Character c = 'a';
        Character anotherCharacter = 'b';
        System.out.println(c.compareTo(anotherCharacter));
    }
}

Output:

-1

No comments:

Post a Comment