Javatpoint Logo
Javatpoint Logo

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

Test it Now

Output:

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

Example 2

Test it Now

Output:

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

Example 3

Test it Now

Output:

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



Help Others, Please Share

facebook twitter pinterest