Unbounded Wildcards vs Raw Types
What you'll learn: Why List<?> provides crucial type safety that raw List completely abandons.
The Critical Difference
Both List<?> and raw List appear to accept "any type," but they are fundamentally different tools with vastly different safety guarantees.
A raw type (List) throws away all generic type information—it's as if generics never existed. The compiler essentially turns off type checking for that reference, allowing dangerous operations that can cause runtime crashes.
An unbounded wildcard (List<?>) says "this is a list of some specific type, but I don't know or care what that type is." The compiler preserves type safety by preventing you from corrupting the collection.
Why Wildcards Win
List rawList = new ArrayList<String>();
rawList.add(42); // Compiles! Dangerous! Heap pollution!
List<?> wildcardList = new ArrayList<String>();
wildcardList.add(42); // Compiler error! ✓ Type safety preserved
wildcardList.add("hi"); // Compiler error! ✓ Still safe
With raw types, you can insert the wrong type and won't discover the problem until runtime. With unbounded wildcards, the compiler blocks all writes (except null), protecting you from corrupting a collection you don't fully understand.
Reading Is Still Safe
List<?> mystery = new ArrayList<String>();
Object item = mystery.get(0); // ✓ Safe—everything is an Object
int size = mystery.size(); // ✓ Safe—doesn't depend on type
You can safely read as Object and call type-independent methods, but you cannot pollute the collection.
Key Takeaway: List<?> enforces "read-mostly" type safety and prevents heap pollution; raw List disables generic type checking entirely and should be avoided in new code.