Character.compare() in Java

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

Syntax:

int java.lang.Character.compare(char x, char y)

This method takes two arguments of type char as its parameters. This method compares two char values numerically.

Parameters: Two parameters are required for this method.

x: the first char to compare.

y: the second char to compare.

Returns:

1. The value 0 if x == y.

2. A value less than 0 if x < y.

3. A value greater than 0 if x > y.

Approach

Java

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

        char x = 'a', y = 'b';
        System.out.println(Character.compare(x, y));
    }
}

Output:

-1

No comments:

Post a Comment