fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Super {}
  8.  
  9. class Sub extends Super {}
  10.  
  11. class GenericArrayHolder<T extends Super>
  12. {
  13. T[] array;
  14.  
  15. @SuppressWarnings("unchecked")
  16. GenericArrayHolder(int n)
  17. {
  18. array = (T[]) new Super[n];
  19. }
  20.  
  21. void set(int i, T t)
  22. {
  23. array[i] = t;
  24. }
  25. }
  26.  
  27. class Test
  28. {
  29. public static void main(String[] args)
  30. {
  31. GenericArrayHolder<Sub> h = new GenericArrayHolder<>(10);
  32. //h.set(3, new Sub());
  33. h.array[3] = null; // ClassCastException
  34. }
  35. }
Runtime error #stdin #stdout #stderr 0.09s 321280KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ClassCastException: [LSuper; cannot be cast to [LSub;
	at Test.main(Main.java:33)