I know that it is impossible to create List<int>
since int is a primitive type. However, I want to know why I can create List<int[]>
. Basically, my question is why it is possible to have Collection<primitive_type_array>
in Java.
According to Java Language Specification
In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
Generics can hold anything that is not a primitive type. Arrays are not primitive types so Collection<int[]>
is allowed. See: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With