Java Generics create array of type-specific generic references

Question

What is the output of the following code?

class Gen<T> {
  T[] vals;/*  w  ww.j  a  v a 2s  . c o  m*/

  Gen(T[] a) {
    vals = a;
    System.out.println(vals.length);
  }
}

public class Main {
  public static void main() {
    Integer n[] = { 1, 2, 3, 4, 5 };  
    
    Gen<Integer> iOb = new Gen<Integer>(n);  
  
    Gen<Integer> gens[] = new Gen<Integer>[10];  
  
    Gen<?> gens2[] = new Gen<?>[10];   
  }
}


Compile time error
Cannot create a generic array of Gen

Note

We cannot create an array of type-specific generic references.




PreviousNext

Related