Tuesday, June 5, 2018

What is the different between int and Integer

http://www.java67.com/2018/02/what-is-difference-between-int-and-integer-in-Java.html

Integer can be null. Bur int cannot be.

int x = null; // This is error

Integer y = null // OK


Only can pass Integer value to the collection class.

List<Integer> element = new ArrayList<>();

we cannot add int value to the element List.

But normally we add value using,
     
       element.add(5);

This work with autoboxing in java, the primitive type int becomes an Integer when necessary.

Autoboxing is the automatic conversion that the java compiler makes between the primitive type and their corresponding wrapper class.

Then 5 convert Integer value by autoboxing.


No comments:

Post a Comment