Int array vs Integer array in java

Int array vs Integer array in java:

Both int array and integer array is same in terms of holding the value/assigning the value. But int[] array can store only primitive data types, where as Integer[] array can store objects also. Also only Integer[] array can store “null”.

 

Int Array:

Int array to store primitive data type (int) values. but now you can store Integer (wrapper class objects) as well because of Autoboxing feature in java (only after java 1.5).

 

 

Integer Array:

Integer Array to store Wrapper class objects (Integer), but now you can store int (primitve data types) as well because of Unboxing feature in java (only after java 1.5).

 

 

Int array vs Integer array Java Program Example:

[java]
public class IntArrayVsIntegerArray {
public static void main(String[] args) {
int a = 10;
Integer b =15;
int[] myIntArray = new int[5]; // Size must be given
myIntArray[0]= a;
myIntArray[0]= b;
System.out.println(“Int Array : “+myIntArray[0]);

int c = 20;
Integer d =10;
Integer[] myIntegerArray = new Integer[5];
myIntegerArray[0] = c;
myIntegerArray[0] = d;
System.out.println(“Integer Array : “+myIntegerArray[0]);
}
}
[/java]

 

Output:
[plain]

Int Array : 15
Integer Array : 10

[/plain]

 

 

Remember:

Integer is a wrapper class and only wrapper classes are allowed inside the collections.

 

In simple words, we can tell like,

int[] => array of primitive values.

 

Integer[] => array of objects.

Leave a Reply