This manual will help you to learn the other methods to compare two integers in Java.
How to Compare two Integers in Java?
For comparing two integers in Java, you can use the below-enlisted methods.
- Comparison operator
- equals() method
- compare() method
Note: The Comparison operator “==” is used to check equality in primitive data types, while for the objects, the equals() method is used. Whereas the compare() method is useful for both primitive data types and integer-type objects.
Let’s see how these methods will work.
Method 1: Compare two Integers in Java Using Comparison Operator
The most commonly used method by programmers to compare two integers is the Comparison operator “==”. It gives “1” if the specified variables are equal; else, it returns “0”.
Syntax
Follow the below-given syntax for comparing two integers using the Comparison Operator:
Example
In this example, we will create two integer type variables “x” and “y” and initialize them:
int y = 23;
Now, compare “x” with “y” using the Comparison Operator in the “if” statements. If the values of both “x” and “y” are the same, the “System.out.println()” method will print the added statement:
System.out.println("Both are equal");
}
else {
System.out.println("x and y are not equal");
}
The output indicates that the “x” and “y” variable values are not equal:
Let’s move toward the other methods for comparing two integers in Java.
Method 2: Compare two Integers in Java Using equals() Method
In Java, for comparing two objects, use the “equals()” method. It outputs the boolean value “true” if both objects are the same; else, it returns “false”. We can also compare two integer objects as a reference by utilizing the “equals()” method.
Syntax
The equals() method has the following syntax:
The equals() method is called with an “x” integer object and will match its value with “y” that is passed as an argument.
Example
Here, we have two integer object references, “x” and “y,” with values “23” and “23”:
Integer y = 23;
Now, we will compare both variables with the help of the “equals()” method in the added “if” condition:
System.out.println("Both x and y are equal");
}else {
System.out.println("x and y are not equal");
}
The output shows that the values of “x” and “y” are equal:
Method 3: Compare two Integers in Java Using compare() Method
The “compare()” method can be also utilized to compare two values numerically. It is the static method that belongs to the “Integer” class. It takes two variables as an argument and returns “0” if the first value is smaller than the second, “-1” if the first value is greater than the second, or “1” in the case of equality.
Syntax
The compare() method uses the following syntax for comparing two integers:
The Integer class is utilized for calling the compare() method, and “x” and “y” are the integers passed as arguments.
Example
Here, we have two integer type variables “x” and “y” with values “23” and “20”:
int y = 20;
We will compare “x” and “y” by using “compare()” method and store the resultant value in “comp” that is an integer type variable:
Now we will check whether the resultant value of “compare()” method is greater than or less than 0 using the following “if” statement.
System.out.println("x is greater than y");
}else if (comp<0){
System.out.println("x is less than y");
}else{
System.out.println("x and y are equal");
}
The output shows that the value of the “x” variable is greater than “y”:
We have provided all necessary information related to comparing two integers in Java.
Conclusion
For comparing two integers in Java, you can use three methods: the Comparison operator, the equals() method, and compare() method. The Comparison operator “==” is used to check equality in primitive data types, while for the objects, the equals() method is used. Whereas the compare() method is useful for both primitive data types and integer-type objects. This manual illustrated the methods to compare two integers in Java with proper examples.