6

I am not able to figure out why ArrayList<int> is not allowed but ArrayList<int[]> is allowed.

I was under the impression that primitive data types are not allowed in Collections, so why is this legit?

3
  • Yeah, primitive data types are not allowed in Collections. Commented Apr 5, 2019 at 6:14
  • 2
    int[] is a reference type (i.e. you can assign an int[] to an Object variable). Commented Apr 5, 2019 at 6:16
  • Object o = new int[10]; Commented Apr 5, 2019 at 6:55

4 Answers 4

7

An array in Java is an object. In Java, we can create arrays by using new operator and we know that every object is created using new operator. Hence we can say that array is also an object.

Collection only works on anything which is Object. int is the primitive datatype and int[] is the Object.

That is the reason ArrayList<int> is not allowed but ArrayList<int[]> is allowed.

Sign up to request clarification or add additional context in comments.

Comments

6

Generics only work for reference type (anything that is an Object).

Primitive int isn't a reference type.

int[] is, as any array is also an Object.

The proper way to deal with multiple int values is to either use just int[] (not putting them into lists), or to use List<Integer>. Which one to pick really depends on your exact use case.

Comments

2

int is primitive.

int[] is object.

ArrayList with ANY_OBJECT is valid.

ArrayList with ANY_PRIMITIVE is not valid.

and as int[] is an object so ArrayList is possible but for int primitive ArrayList is INVALID.

Comments

0

An array in Java is an object. In Java, we can create arrays by using new operator and we know that every object is created using new operator.

In Java, there is a class for every array type, so there’s a class for int[] and similarly for float, double etc. The direct superclass of an array type is Object. Every array type implements the interfaces Cloneable and java.io.Serializable. All methods of class Object may be invoked on an array. This can be checked from below code :

public class Test { 
public static void main(String[] args) 
{  
    System.out.println(args instanceof Object); 
    int[] arr = new int[2]; 
    System.out.println(arr instanceof Object); 
 } 
} 

Output : True True

The diamond operator used in ArrayList initialisation specifies a generic type. A generic type is a generic class or interface that is parameterised over types.

You can go through the source code for ArrayList here: http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/java/util/ArrayList.java

You can see that the type of elementData in parameterised constructor is Object. A primitive is a data type which is not an object :

private transient Object[] elementData;

So as int is primitive data type in java it cannot be used as Generic type where as int[] , that has direct superclass Object, can be.

You can read more about it here: https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

2 Comments

"This can be checked from below code :" but System.out.println(args instanceof Object[]); would also be true, even though Object[] is not a superclass of String[]. (It is a supertype, though).
@AndyTurner System.out.println(args.getClass().getSuperclass()); will output java.lang.Object. So class of a Sting[] is java.lang.String where as superclass is java.lang.Object which also makes it a supertype. However the vice versa is not true. A supertype may or may not be a superclass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.