Why do generics in Java work with classes but not with primitive types?
For example, this works fine:
List foo = new ArrayList
When creating an object, you cannot substitute a primitive type for the type parameter.As to the why of this restriction, it is a compiler implementation issue. Primitive types have their own bytecode instructions for loading and storing onto the virtual machine stack. So it is not impossible to compile primitive generics into these separate bytecode paths, but it would make the compiler complicated.