userimage

Why Generics supports Objects only not primitives ?

Suppose I will write like,

ClassOne<Integer> classOne = new ClassOne<Integer>(); // Will work fine WRT Generics

ClassOne<Integer> classOne = new ClassOne<Integer>(); // This statement will not allowed by Generics

So my question why Generics in Java work with objects but not with primitive types?

userimage

Generics in Java are an entirely compile-time construct - the compiler turns all generic uses into casts to the right type. This is to maintain backwards compatibility with previous JVM runtimes.

In Java, generics work the way that they do ... at least in part ... because they were added to the language a number of years after the language was designed.

The language designers were constrained in their options for generics by having to come up with a design that was backwards compatible with the existing language and the Java class library.

Answer is