Why primitive datatypes are not allowed in java.util.ArrayList?
Because Java can only use class (and not primitive types) and arrays (also arrays for primitives) for generics (between < and >).
List list; That is also a reason why there are wrapper classes for primitive types:
int -> Integer
boolean -> Boolean
double -> Double
byte -> Byte etc...
All collection classes of java store memory location of the objects they collect. The primitive values do not fit in to the same definition.
To circumvent this problem, JDK5 and onwards have autoboxing - wherein the primitives are converted to appropriate objects and back when they are added or read from the collections. Refer to the official JDK tutorial on this topic.
Checking the JDK5 source code for ArrayList helps better understanding: creating an ArrayList<E>
includes casting an Object[]
array to E[]
.