What Are Raw Types?
You'll learn: what a raw type is, why it exists in Java, and when you might encounter it in legacy code.
The Core Idea
A raw type is a generic class or interface used without any type arguments. Instead of writing ArrayList<String> (a parameterized type), you write just ArrayList (a raw type).
When Java 5 introduced generics in 2004, millions of lines of pre-generics code already existed. To prevent all that code from breaking overnight, the language designers allowed generic classes to be used without type parameters—creating raw types. This ensured backward compatibility.
Raw Type vs Parameterized Type
// Parameterized type (modern, type-safe)
ArrayList<String> names = new ArrayList<String>();
// Raw type (legacy, not type-safe)
ArrayList names = new ArrayList();
When you use a raw type, the compiler essentially treats it as if generics don't exist. You lose all type safety—any object can go in, and you must cast when retrieving items.
Why Raw Types Exist
Before generics, collections held Object references. Code like this was common:
ArrayList list = new ArrayList(); // pre-Java 5 style
list.add("hello");
String s = (String) list.get(0); // manual cast required
Raw types let this old code still compile and run with newer Java versions, even though ArrayList is now a generic class.
Don't Use Them in New Code
Raw types bypass the compiler's type-checking safeguards. They're a compatibility feature, not a best practice. Always use type arguments in modern Java.
Key Takeaway: Raw types are generic classes used without angle brackets or type arguments—they exist purely for backward compatibility with pre-generics code and should be avoided in new projects.