Java Integer compare() method

The compare() method is a method of Integer class under java.lang package. This method compares two integer values numerically. It returns the result in integer equivalent value by comparing the two int method arguments. The value returned is identical to what would be returned by:

Integer.valueOf(x).compareTo(Integer.valueOf(y))

Syntax

Following is the declaration of compare() method:

Parameter:

DataTypeParameterDescriptionRequired/Optional
intxThe first integer value to compareRequired
intyThe second integer value to compareRequired

Returns:

This method will returns the following values:

Exceptions:

NA

Compatibility Version:

Java 1.7 and above

Example 1

Output:

-1
0
1

Example 2

Output:

Enter the first numeric value: 55
Enter the second numeric value: 66
number1 is less than number2

Example 3

Output:

1
0
-1