45

I want to declare an ArrayList of type int.

Why does the following give me an error:

ArrayList<int> list1 = new ArrayList<int>(); 

But the following works:

ArrayList<Integer> list1 = new ArrayList<Integer>(); 

?

3
  • 4
    Because int isn't an object in the same context the Integer is Commented Jan 15, 2013 at 23:43
  • 1
    You can use TIntArrayList if you want something which wraps int[] Commented Jan 15, 2013 at 23:50
  • int is primitive datatype we can use wrapper class in ArrayList Commented Jan 11, 2018 at 12:12

7 Answers 7

41

ArrayList can only reference types, not primitives. Integer is a class, not a primitive.

When you declare ArrayList<Integer> list1 = new ArrayList<Integer>(), you're creating an ArrayList which will store the Integer type, not the int primitive.

If you want to read about the difference between primitive and reference types, check out http://pages.cs.wisc.edu/~hasti/cs302/examples/primitiveVsRef.html

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

1 Comment

You probably don't need the <Integer> type argument on the right of the new expression. ArrayList<Integer> list1=new ArrayList(); works too.
14

Because int is a primitive type. Only reference types can be used as generic parameters.

4 Comments

Even before generics (before Java 5) this restriction was there so I doesn't think its the only reason
@Pangea: Sure, you can't do new ArrayList().add(5). But the OP is asking about the error wrt that particular line of code...
@OliCharlesworth You can do that actually. It just will be adding an Integer not an int value.
@PeterLawrey: Ah, you're right. I guess this didn't work prior to autoboxing?
12

The short answer is that generics (like ArrayList<Integer>) do not accept primitive types (int), only objects (Integer).

This is because classes like ArrayList are implemented as using Objects. Since every class inherits from Object, the compiler can just plug in other classes. But primitive types (like int) do not inherit from Object, for they are not classes. So, Sun/Oracle made the Integer class to help with this.

So, in short: int is not an Object.

1 Comment

This is the best answer because it explains this declaration, which works fine: ArrayList<int[]>
1

All the answers above answer why but the root of this question is frequent auto boxing and unboxing of the primitive data types. This problem is already solved by IntBuffer or ChadBuffer or you name the primitive type it's already there in the nio folder. Next time if you want to use primitive ArrayList don't use List instead use IntBuffer

Comments

0

int is a primitive data type but Integer is a class so an arrayList array can only take reference types as its parameter not primitive type

Comments

0

int is not an Object and hence if list type is int, implementations of the list cannot be done.

Comments

0

int is a primitive. It is not a Object.

Refer this link for further details.

1 Comment

Link only answers are not encouraged at StackOverflow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.