2

Java does not support creating a collection out of primitives, so following construct gives compilation error ("The argument can not be of primitive type):

 List<int> ints = new ArrayList<int>(); 

On the other hand creating a collection of arrays or primitives is allowed, so following construct is ok:

List<int[]> ints = new ArrayList<int[]>();

What is a logic behind this?


Edit: The question is really about the array of primitives, not the primitives, so please don't explain me why can't I store primitives in collection, but rather why can I story array of primitives inside a collection?

walkeros
  • 4,736
  • 4
  • 35
  • 47
  • 2
    Because an array is an object, not a primitive. – Axel Oct 21 '16 at 07:34
  • @Axel: Where it is said that array of primitives is an object? – walkeros Oct 21 '16 at 07:37
  • @BackSlash: This is not really the answer, it does not say wheather array of primitives is object or not. – walkeros Oct 21 '16 at 07:37
  • An array is an object regardless of whether it holds objects or primitives – Mark Oct 21 '16 at 07:38
  • @MarkKeen: Where is it defined? – walkeros Oct 21 '16 at 07:39
  • 3
    http://stackoverflow.com/a/8781051/4252352 – Mark Oct 21 '16 at 07:40
  • It's fantastic how admins tend to mark question on stackoverflow as duplicates without really understanding them – walkeros Oct 21 '16 at 07:52
  • @MarkKeen: Thans for your link. Actually it finally pointed me to the actual reference [https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html ] where it is said that (as you and Axel wrote array is Object). This actually answers my question, Thanks – walkeros Oct 21 '16 at 08:05
  • @walkeros did my answer help you? If yes, please mark it as an answer. Otherwise, maybe it's time to launch your first bounty? – xenteros Oct 24 '16 at 14:44
  • xenteros: I already added a comment under your answer. But the real answer was given in a comment by Mark Keen. I'm willing to accept this as an answer but @MarkKeen would have to put it as an answer. – walkeros Oct 25 '16 at 11:55
  • Although I knew the answer the credit has to go to the Paul (on link I provided) as this has already been answered before - why I didn't duplicate as an answer. Personally @xenteros answer, with the edit, appears to answer your initial question - so mark as correct if you wish (I up voted myself). Although it was already answered in comments before the edit, but thats just semantics ... – Mark Oct 25 '16 at 16:12
  • 1
    Possible duplicate of [Is an array an object in java](http://stackoverflow.com/questions/8781022/is-an-array-an-object-in-java) – ivan_pozdeev Oct 27 '16 at 21:34

2 Answers2

6

Collections are generic: Collection<T>. T must be reference type. Primitives aren't reference types. On the other hand, array of primitives is a reference type so you can put it to Collection. Remember, that every primitive has it's wrapper class which can be passed as a type to generic type.

According to the specification:

Type:
    PrimitiveType
    ReferenceType

A class is generic if it declares one or more type variables (§4.4).

#

4.3.1. Objects An object is a class instance or an array.

#

4.4. Type Variables A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • That's true, however this is not complete answer I think, since generics live only until compilation time. In fact if you dony use generic type you can write something like: `List a = new ArrayList(); a.add(new int[0]); a.add(0);`. The first will be stored as array itself and the second will be autoboxed and NOT stored as a primitive. – walkeros Oct 21 '16 at 08:03
1

An object is a class instance or an array. The Java Language Specification mentions: 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.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#:~:text=In%20the%20Java%20programming%20language,be%20invoked%20on%20an%20array.

Hence arrays can be used with Collections.

rvp
  • 580
  • 4
  • 7
  • 21