Java

How to Compare two Integers in Java

In Java, “Integer” is a wrapper class by the java.lang package used for constructing integer objects. It stores integer values in 128 bits. While programming in Java, there exists a chance that you need to compare two values of the same data type, such as int. Java offers different methods to compare two Integers; however, the most common method used is the Comparison Operator.

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:

x == y

Example
In this example, we will create two integer type variables “x” and “y” and initialize them:

int x = 20;
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:

if(x==y) {
            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:

x.equals(y)

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 x = 23;
Integer y = 23;

Now, we will compare both variables with the help of the “equals()” method in the added “if” condition:

if(x.equals(y)) {
            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:

Integer.compare(x, y)

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 x = 23;
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:

int comp = Integer.compare(x, y);

Now we will check whether the resultant value of “compare()” method is greater than or less than 0 using the following “if” statement.

if(comp>0) {
         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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.