Java Integer compare() Method
Example
Compare two numbers:
public class Main {
public static void main(String[] args) {
System.out.println(Integer.compare(5, 10));
System.out.println(Integer.compare(10, 10));
System.out.println(Integer.compare(10, 5));
}
}
Definition and Usage
The compare() method compares two int values.
It returns 0 if the numbers are equal, a number less than 0 if the first number is smaller, and a number greater than 0 if the first number is larger.
Tip: To compare two Integer objects, you can also use the compareTo() method.
Syntax
public static int compare(int x, int y)
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The first number to compare. |
| y | Required. The second number to compare. |
Technical Details
| Returns: | An int: 0 if x == y, less than 0 if x < y, and greater than 0 if x > y. |
|---|
Related Pages
❮ Integer Methods