Javatpoint Logo
Javatpoint Logo

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:

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

Test it Now

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

Test it Now

Output:

1
0
-1

Next TopiccompareTo() Method




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA