Java Integer Equals Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the equals() method of the Integer class in Java. This method is used to compare the value of an Integer object with the value of another object. In this lab, you will learn how to use the equals() method and how it works.

Create an Integer Object

In this step, create an Integer object and assign a value to it. Then, create another Integer object with the same value.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = 25;
        Integer num2 = 25;

    }
}

Compare Two Integer Objects Using equals() Method

In this step, compare the two Integer objects using the equals() method. The equals() method returns true if the value of the Integer object is equal to the value of the parameter passed to it.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = 25;
        Integer num2 = 25;

        if(num1.equals(num2)) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is equal to num2

Compare Two Different Integer Objects

In this step, create two different Integer objects with different values and then compare them using the equals() method.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = 25;
        Integer num2 = 30;

        if(num1.equals(num2)) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is not equal to num2

Check Equals Method for Null

In this step, check if the equals() method works correctly when compared to null.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = null;
        Integer num2 = 25;

        if(num1 != null && num1.equals(num2)) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is not equal to num2

Compare Two Integer Objects Using == Operator

In this step, compare two Integer objects using the == operator. The == operator compares the reference of the objects.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = 25;
        Integer num2 = 25;

        if(num1 == num2) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is equal to num2

Create an Integer Object from a String

In this step, create an Integer object by parsing a String value.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = Integer.parseInt("25");
        Integer num2 = 25;

        if(num1.equals(num2)) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is equal to num2

Compare an Integer Object with an Object of Another Class

In this step, compare an Integer object with an object of another class using the equals() method.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = 25;
        String str = "25";

        if(num1.equals(str)) {
            System.out.println("num1 is equal to str");
        } else {
            System.out.println("num1 is not equal to str");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is not equal to str

Create an Integer Object from a Binary Value

In this step, create an Integer object from a binary value using the parseInt() method.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = Integer.parseInt("1010", 2);
        Integer num2 = 10;

        if(num1.equals(num2)) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is equal to num2

Use Autoboxing for Integer Objects

In this step, use autoboxing to create Integer objects.

// ~/project/IntegerEqualsMethod.java

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

        int num1 = 25;
        int num2 = 25;

        Integer intObj1 = num1;
        Integer intObj2 = num2;

        if(intObj1.equals(intObj2)) {
            System.out.println("intObj1 is equal to intObj2");
        } else {
            System.out.println("intObj1 is not equal to intObj2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

intObj1 is equal to intObj2

Compare Two Integer Objects with Different Values

In this step, compare two Integer objects with different values.

// ~/project/IntegerEqualsMethod.java

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

        Integer num1 = 25;
        Integer num2 = 30;

        if(num1.equals(num2)) {
            System.out.println("num1 is equal to num2");
        } else {
            System.out.println("num1 is not equal to num2");
        }

    }
}

To run the code, use the following command in the terminal:

javac IntegerEqualsMethod.java && java IntegerEqualsMethod

Output:

num1 is not equal to num2

Summary

In this lab, you learned how to use the equals() method of the Integer class to compare two Integer objects. You also learned how the equals() method works and how to create an Integer object from different sources such as String value, binary value, and using autoboxing.

Other Java Tutorials you may like