Compare Integer And Int Java

Compare Integer And Int Java | Here we see how to compare Integer and int in Java programming language. Both int and Integer seem the same but the main difference between them is int is a primitive data type whereas Integer is a Wrapper class.

As int is a primitive data type it has less flexibility it can have only the binary value of an integer in it, but as an Integer is a wrapper class it has more flexibility in converting, manipulating, and storing the int data type.

Can We Compare Int And Integer In Java?

Yes, we can compare the int primitive data type and the Integer wrapper class value in Java. The below example for the same is as follows:-

When we declare the variables using int and Integer they work the same. Due to auto-boxing, the primitive value is converted to an Integer wrapper class value. While comparison, due to auto-unboxing Integer wrapper class value is converted to an int primitive value, and then it is compared with other int values.

int var1 = 45;
Integer var2 = 45;

if(var1 == var2) {
    // statement
}

Java Compare Integer And Int

We can create an Integer class object by using Integer.valueOf() method where we have to pass an int value to the valueOf() method. And we can create int primitive value by normal variable assignment. After that we can compare Integer and Int using ==, >=, <=, >, <.

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

        Integer var1 = Integer.valueOf(321);
        System.out.println(var1);
        int var2 = 321;
        System.out.println(var2);

        if (var1 == var2) {
            System.out.println("Both var1 & var2 are equal.");
        } else {
            System.out.println("var1 & var2 are not equal.");
        }
    }
}

Output:-

321
321
Both var1 & var2 are equal.

Ex-2:- Compare Integer And Int Java

As we have mentioned earlier wrapper class that is Integer type has more flexibility than the int type, now in the below program we demonstrate it.  Since we have the concept of auto-boxing we can convert the wrapper to primitive data type and primitive data type to wrapper class and perform arithmetic operations between these two. 

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

        Integer var1 = Integer.valueOf(21);
        Integer var2 = Integer.valueOf(31);
        int var3 = 4;
        double var4 = 5.1;
        Double var5 = Double.valueOf(22.1);
        int var6 = var1 + var2;
        System.out.println("Sum of 2 Integer objects: " + (var1 + var2));
        System.out.println("Sum of an Integer object and int value: " 
                           + (var1 + var3));
        System.out.println("Sum of an Integer object and double value: " 
                           + (var1 + var4));
        System.out.println("Sum of an Integer object and Double object: " 
                           + (var1 + var5));
        System.out.println("Sum of 2 Integer objects assigned to int variable: "
                           + var6);
    }
}

Output:-

Sum of 2 Integer objects: 52
Sum of an Integer object and int value: 25
Sum of an Integer object and double value: 26.1
Sum of an Integer object and Double object: 43.1
Sum of 2 Integer objects assigned to int variable: 52

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *