Java Generics array instantiate

Question

What is the output of the following code?

class Gen<T> {
  T vals[];//w  w  w  .j a va2 s.  c om

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

public class Main {
  public static void main() {
    Gen<String> g = new Gen<>();
    Gen<Integer> g2 = new Gen<>();
  }
}


Compile time error
Cannot create a generic array of T

Note

We cannot instantiate an array whose element type is a type parameter.




PreviousNext

Related