In this tutorial , we will discuss java integer compare method . Integer.compare() method is use to integer comparison in java . First we discuss about syntax and example of java Integer.compare() method then see how to compare two integers in java using Integer.compare() method .
Java Integer.compare() Method :
Java Integer.compare() method is use to compares two int values numerically. It takes two parameters and return result of comparison of integer , which are given as parameter .
Syntax :
Integer.compare(int x, int y)
Parameter : It takes two parameter x and y. x the first int to compare and y the second int to compare
Return Value : It’s return numeric value as
- If x>y then the method returns an int value greater than zero.
- If x=y then the method returns zero.
- And If x<y then the method returns an int value less than zero.
Now we will see different example of java integer compare() method .
Example 1 : Java Integer.compare() Example when x> y
In this example we will initialize two int variable x and y (x>y). we will compare two integers x and y in java using Integer.compare() method and result will store in result variable . There is result of integer comparison should be greater then zero and we will print this result of integer comparison in our program.
public class JavaIntegerCompare {
public static void main(String[] args) {
int x = 5;
int y = 2;
//compare int x and y using compare method
int result = Integer.compare(x, y);
//print result of compare int x and y
System.out.println("Result of compare("+ x +", "+ y +" )"+ " = "+ result);
}
}
Output:
Result of compare(5, 2 ) = 1
Example 2 : Java Integer.compare() Example when x < y
In this example we will initialize two int variable x and y (x<y) . Now we will compare two integers x and y in java using Integer.compare() method and result will store in result variable . There is result of integer comparison should be less then zero and we will print this result of integer comparison in our program.
public class JavaIntegerCompare1 {
public static void main(String[] args) {
int x = 2;
int y = 5;
//compare int x and y using compare method
int result = Integer.compare(x, y);
//print result of compare int x and y
System.out.println("Result of compare("+ x +", "+ y +" )"+ " = "+ result);
}
}
Output :
Result of compare(2, 5 ) = -1
Example 3 : Java Integer.compare() Example when x = y
Now we will take two int variable x and y and initialize as x=y . Then we will compare two integers x and y in java using Integer.compare() method and result will store in result variable . Here result of integer comparison should be equal to zero .
public class JavaIntegerCompare2 {
public static void main(String[] args) {
int x = 5;
int y = 5;
//compare int x and y using compare method
int result = Integer.compare(x, y);
//print result of compare int x and y
System.out.println("Result of compare("+ x +", "+ y +" )"+ " = "+ result);
}
}
Output :
Result of compare(5, 5 ) = 0
In this tutorial we have learned how to compare two integers in java using integer.compare() method . We have seen different example for java compare int . java integer compare() method return numeric value on the bases of given parameter. You can see more java program for practice .