Int vs Integer in Java

Int vs Integer in Java | In this section, we will see what’s the difference int vs integer in Java. Even though both seem to be the same but they are not. The int is a primitive data type whereas the Integer belongs to the wrapper class. Wrapper class is those that allow you to use the primitive data types as an object hence Integer here will be an object. The Integer class is defined in java.lang package.

Java Integer vs Int

Integer when compared to int has more flexibility it can store, manipulate and convert the int data but int can only store the data. Again when compared to int, Integer has more built-in methods. How to declare int and Integer:-

int a = 100; // valid
Integer a = 89; // valid

Since int is a primitive type therefore we can store value in an int-type variable by assignment. And due to the autoboxing features of Java programming language, we can directly assign value to an Integer object. Internally it will call Integer.valueOf() method to create the Integer class object.

public class Main {
    public static void main(String[] args) {
        Integer n1 = 10;
        Integer n2 = Integer.valueOf(50);

        System.out.println(n1);
        System.out.println(n2);
    }
}

Output:-

10
50

Since the Integer is a class having built-in methods therefore we can call those methods on an Integer object. But int is a primitive therefore we can’t call methods directly on it.

public class Main {
    public static void main(String[] args) {
        Integer n1 = 99;
        int n2 = 100;

        System.out.println(n1.getClass().getName());
        System.out.println(n1.toString());
        System.out.println(n1.doubleValue());
        System.out.println(n1.compareTo(500));

        // Error
        // System.out.println(n2.getClass().getName());
    }
}

Output:-

java.lang.Integer
99
99.0
-1

Also see:- Convert int to Integer, convert Integer to int.

When To Use Int vs Integer in Java

You might be thinking when an int primitive is already there to store numbers (non-floating point value) then why Java has an Integer class to store the same value as an object? The main reason for this is Java Collection.

All Java collection classes, support only Objects but not primitive values. Therefore if we want to store some primitive value then we have to take support of its wrapper classes. For every primitive type, Java contains wrapper classes. For int primitive, it has an Integer class.

List<Integer> list = new ArrayList<>(); // valid
List<int> list = new ArrayList<>(); // invalid
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(50);
        list.add(90);
        System.out.println(list);
    }
}

Output:-

[10, 50, 90]

So, when to use int vs integer in Java? If you are working with Java Collections then you can use the Integer class else you can use the int primitive value.

Int vs Integer Java Performance

While considering the performance int gives better performance because int utilizes less memory space when compared to an Integer and hence int will be faster than Integer. Therefore when you are considering the performance it is better to use int.

Wrapper

The Integer is just a wrapper of int value, internally it will be holding only an int value. For example:- the above picture represents wrapping a box.

If we have to perform some basic operations on values then int can be used. But if we want to use built-in Java methods while performing some operations then Integer can be used.

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 *