views:

82

answers:

2

Why does this one does not work:

ArrayList<LinkedList<int>> 

where this one does:

ArrayList<LinkedList<Integer>> 

???

A: 

The argument in the <> must be an object because those classes can only hold objects.

int is a primitive type, where as Integer is simply a wrapper class for that type, so Integer is the one that will work.

Chris Cooper
yes i got it :) i thoght it does work for LinkedList<int> so it made me crazy ' just got confused with C++.
yoavstr
+6  A: 

Because Java can only use class (and not primitive types) and arrays (also arrays for primitives) for generics (between < and >).

List<Integer> list;

That is also a reason why there are wrapper classes for primitive types:

int -> Integer
boolean -> Boolean
double -> Double
byte -> Byte
etc...
Martijn Courteaux
it will accept primitive arrays, though.
Tedil