Sobes.tech
Junior — Middle

What prevents the use of basic data types in generics parameters?

sobes.tech AI

Answer from AI

In Java, generics are implemented using type erasure, which means that information about the parameter types of generics is not available at runtime.

Because of this, primitive types (int, boolean, etc.) cannot be used as generic parameters, as they are not objects and cannot be represented as a class.

For example, you cannot write:

List<int> numbers; // Error

Instead, you should use wrappers:

List<Integer> numbers; // Correct

This is because generics only work with object types, and primitives are not objects.

What prevents the use of basic data types in generics… - sobes.tech