Sobes.tech
Middle+

What do you know about heap pollution?

sobes.tech AI

Answer from AI

Heap pollution is a situation in Java where objects of incompatible types are inserted into a parameterized (generic) type due to unsafe operations with generics, especially during type casts.

For example, using raw types or unsafe casts can prevent the compiler from guaranteeing type safety, leading to a ClassCastException at runtime.

Example of heap pollution:

List rawList = new ArrayList<String>(); // raw type
rawList.add(10); // compiler does not complain
List<String> stringList = rawList; // heap pollution
String s = stringList.get(0); // ClassCastException at runtime

To avoid heap pollution, it is recommended to avoid using raw types, use @SuppressWarnings annotations cautiously, and adhere to type-safe operations with generics.

What do you know about heap pollution? — Android - sobes.tech