Integer compare()
Java Integer compare() method
Last Updated : 17 Mar 2025
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:
| DataType | Parameter | Description | Required/Optional |
|---|---|---|---|
| int | x | The first integer value to compare | Required |
| int | y | The second integer value to compare | Required |
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
Next TopicJava-integer-compareto-method