Java character compareTo() method

The compareTo(character another character) method of character class is used to compare two objects numerically.

Syntax

Parameter

The above method requires only one parameter:

The character that need to be compared.

Return

The compare(char x, char y) method of character class returns :

The value 0 if the given Character is equal to the argument Character.

A value less than 0 if the given Character is numerically less than the Character argument

A value greater than the given Character is numerically greater than the Character argument.

Example 1

Output:

The first value 'g' is greater than the second value 'G'.

Example 2

Output:

The first value 't' is greater than the second value 'G'.

Example 3

Output:

The first value is greater than the second value
The first and third value are equal