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