Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primitive arrays as a generic parameter [duplicate]

Tags:

java

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.

like image 584
dmitryvinn Avatar asked Feb 07 '23 12:02

dmitryvinn


2 Answers

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.

like image 104
Radu Ionescu Avatar answered Feb 14 '23 00:02

Radu Ionescu


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

like image 22
Neil Locketz Avatar answered Feb 13 '23 22:02

Neil Locketz