Compare Two Integers in Java

Mohammad Irfan Jan 30, 2023 Apr 11, 2021
  1. Compare Two Integer Values Using the == Operator in Java
  2. Compare Two Integer References Using the equals() Method in Java
  3. Compare Two Integers Using equals() Method in Java
  4. Don’t Compare Two Integer References Using the == Operator in Java
Compare Two Integers in Java

This tutorial introduces how to compare two integers in Java.

To compare integer values in Java, we can use either the equals() method or == (equals operator). Both are used to compare two values, but the == operator checks reference equality of two integer objects, whereas the equal() method checks the integer values only (primitive and non-primitive).

So, while comparing the integer values, it is up to the developers to choose between the comparison methods. Let’s see some examples.

Compare Two Integer Values Using the == Operator in Java

In this example, we take two primitive integers, and then by using the == operator, we compare both values. We test this example using Java 15. See the example below.

public class SimpleTesting{
    public static void main(String[] args) {
        int a = 18;
        int b = 18;
        if(a==b) {
            System.out.println("Both are equal");
        }else System.out.println("Not equal");
    }
}

Output:

Both are equal

Compare Two Integer References Using the equals() Method in Java

We can use the equals() method to compare two integers in Java. It returns true if both objects are equal; otherwise, it returns false. See the example below.

public class SimpleTesting{
    public static void main(String[] args) {

        Integer a = new Integer(18);
        Integer b = new Integer(18);
        if(a.equals(b)) {
            System.out.println("Both are equal");
        }else System.out.println("Not equal");
        
    }
}

Output:

Both are equal

Compare Two Integers Using equals() Method in Java

Here, we are comparing two integer references by using the equals() method.

public class SimpleTesting{
    public static void main(String[] args) {
        Integer a = 10;
        Integer b = 10;
        if(a.equals(b)) {
            System.out.println("Both are equal");
        }else System.out.println("Not equal");
    }
}

Output:

Both are equal

Don’t Compare Two Integer References Using the == Operator in Java

We should not use the == operator to compare two integer values because it checks the equality of the reference.

Java caches the Integer value in the range of -128 to 127. Therefore, when two integer objects have the same value in this range, the == comparator will return true because they refer to the same object. But it will return false for any value outside of this range.

public class SimpleTesting{
    public static void main(String[] args) {
        Integer a = 18;
        Integer b = 18;
        if(a==b) {
            System.out.println("Both are equal");
        }else System.out.println("Not equal");
    }
}

Output:

Both are equal
public class SimpleTesting{
    public static void main(String[] args) {
        Integer a = 150;
        Integer b = 150;
        if(a==b) {
            System.out.println("Both are equal");
        }else System.out.println("Not equal");
    }
}

Output:

Not equal

As you can see above, we shall not use the == to compare two Integer values.

Related Article - Java Integer