Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics syntax: classes versus primitive data types

Why does this one does not work:

ArrayList<LinkedList<int>> 

where this one does:

ArrayList<LinkedList<Integer>> 

???

like image 546
yoavstr Avatar asked Feb 07 '26 02:02

yoavstr


2 Answers

Because Java can only use classes (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...
like image 161
Martijn Courteaux Avatar answered Feb 08 '26 17:02

Martijn Courteaux


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.

like image 44
Chris Cooper Avatar answered Feb 08 '26 16:02

Chris Cooper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!