Compare two integers in Java

  1. Compare Two Integer Values using the == Operator in Java
  2. Compare Two Integer References using the == Operator in Java
  3. Compare Two Integer References using the equals() Method in Java
  4. Compare Two Integers using equals() Method 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 == Operator in Java

In this example, we compare two integer references by using the == operator. It is the case when we have integer references rather than primitive values. Java autoboxes the values into an object. See the example below.

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

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
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java int

  • Convert a String to an Int in Java
  • Related Article - compare integers

  • Convert a String to an Int in Java