0

What I don not understand is why Java allows this Generic<Integer> lonelyObject=new Generic<Integer>(); and not this:

Genericd<Integer>[] arrayObject= Generics<Integer>[10];

If the second one looses type safety due to invariance, shouldn't the first be the same?

Thanks for all the replies! I understand why it does not allow arrays to be created, because they contain type info at runtime and due to type erasure there are none for Generics at runtime, but how does this violates type safety and creating a simple object doesn't?

Could somebody please explain.

  • check this https://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java – Yohannes Gebremariam Aug 25 '17 at 19:05
  • Thanks! I understand why it does not allow arrays to be created, because they contain type info at runtime and due to type erasure there are none for Generics at runtime, but how does this violates type safety and creating a simple object doesn't? – Kumar Kavish Aug 25 '17 at 19:11

0 Answers0