Java Character compare() Method

The compare(char x, char y) method of Character class is used to compare two char values numerically. The final value returned is similar to what would be returned by:

Syntax

Parameter

The above method requires two parameters:

  • char x which is the first character to compare
  • char y which is the second character to compare

Return Value

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

  • a value 0 if x==y
  • a value less than 0 if x<y.
  • a value greater than 0 if x>y.

Example 1

Output:

First value is less than the second value.

Example 2

Output:

Value 1 is greater than the value 2.

Example 3

Output:

First and second values are equal.
First value is less than the third value.