Note that there are some explanatory texts on larger screens.

plurals
  1. POjava arrays of parametric types
    primarykey
    data
    text
    <p>This is <strong>not</strong> a question how to do things. It is a question of <strong>why</strong> is it the way it is.</p> <p>Arrays in Java know their component type at run time, and because of type erasure we cannot have array objects of generic type variables. Array types involving generics are allowed and checked for sound read/write, the only issue seem to be the allocator expressions.</p> <p>Notice that the Java compiler also disallows the following:</p> <pre><code>Pong&lt;Integer&gt; lotsofpong [] = new Pong&lt;Integer&gt;[100]; </code></pre> <p>...where <code>Pong</code> is just any old parametric class. There is nothing unknown here. Yes, at run-time, <code>lotsofpong</code> would just be an array of <code>Pong</code>, but I cannot see a reason why the compiler cannot remember the type parameter for compile-time purposes. Well, it actually does remember it, because those types exist at compile time, so the only problem seems to be the refusal to give the allocator at compile-time a particular generic-parameter-involving component type.</p> <p>Even if the parameter of <code>Pong</code> was a generic type variable that should not make a difference either. The dynamic array would still be an array of <code>Pong</code>, requiring per element the size of a <code>Pong</code>, which does not depend on its type parameter.</p> <p>Yes, I know there are ways around it - either use casts (perhaps with SuppressWarning) from the non-parametric type, or subclass <code>Pong&lt;Integer&gt;</code> with a non-parametric class and use that type instead. But is there a reason why this kind of allocator is not allowed?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload