Course contentsShow
← Java
Lesson 883 of 2,87021. Generics FundamentalsFree lesson

Raw Type Syntax and Declaration

How to declare and instantiate raw types by omitting angle brackets and type parameters from generic classes.

Raw Type Syntax and Declaration

You'll learn how to write and create raw types by leaving out the angle brackets and type arguments from generic classes.

The Core Idea

When you use a generic class without specifying its type arguments, you create what's called a raw type. Instead of writing ArrayList<String> or Box<Integer>, you simply write ArrayList or Box. The angle brackets disappear entirely, along with any type parameters inside them.

This is the syntax Java used before generics were introduced in Java 5, and it still works today for backward compatibility—but it's generally discouraged in modern code.

Declaration and Instantiation

Here's how raw type syntax looks compared to parameterized types:

// Parameterized type (modern, type-safe)
Box<String> properBox = new Box<String>();
Box<String> properBoxWithDiamond = new Box<>();

// Raw type (legacy syntax, no angle brackets)
Box rawBox = new Box();

You can also declare raw type references and variables:

// Raw type variable declaration
Box rawReference;

// Raw type with multiple type parameters omitted
Map rawMap = new HashMap();  // instead of Map<K, V>

Notice that both sides of the declaration can use raw type syntax. You're completely omitting the <TypeArgument> portion that would normally follow the class name.

Why It Exists

Raw types exist solely for backward compatibility with pre-generics Java code (before Java 5). They allow old code to compile alongside modern generic code, but they sacrifice all the type safety benefits that generics provide.

Key Takeaway: Raw type syntax omits angle brackets entirely—write just the class name (Box instead of Box<T>) when declaring or instantiating. This legacy feature bypasses generic type safety and should be avoided in new code.