Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Raw `List` and `ArrayList`

Raw `List` and `ArrayList` bypass compile-time element-type checks. Assigning a raw list to `List<String>` neither validates its contents nor recovers a type argument.

Version: Java SE 21

Syntax

import java.util.ArrayList;
import java.util.List;

class RawTypesSyntax {
    // Raw types: no type argument list
    List raw = new ArrayList();

    // Parameterized types: <> tells the compiler to infer String
    List<String> strings = new ArrayList<>();

    // Unknown element type, not a raw type
    List<?> unknown = strings;

    // Object is an explicit element type, not a raw type
    List<Object> objects = new ArrayList<>();
}

In Java SE 21, List<E> is a generic interface and ArrayList<E> is a class that implements it. In List raw = new ArrayList();, the variable type and the constructed type are both raw. This is what raw List and ArrayList means here: both type argument lists are omitted. Raw types exist for compatibility with legacy code. In List<String> strings = new ArrayList<>();, <> is not raw syntax: the compiler infers String.

Through a raw reference, erasure makes add accept Object rather than enforce a specific element type, so the call produces an unchecked warning. The static return type of get(int) is Object, but the call itself produces no such warning because its int parameter is unchanged. With javac, -Xlint:rawtypes,unchecked shows both raw-type uses and unchecked operations.

Assigning a raw list to List<String> is an unchecked conversion. It does not check or copy the elements, and it does not recover a type argument. Both references point to the same list. If a retrieved element is incompatible with String, the compiler-inserted cast can throw ClassCastException.

Use List<String> when the element type is known. List<?> represents an unknown element type: you can read elements as Object, but you cannot add a non-null element through that reference. List<Object> explicitly specifies Object as the element type and accepts values of different reference types without an unchecked warning. If a legacy API returns a raw list, validate its elements before copying them into a parameterized list.

Examples

import java.util.ArrayList;
import java.util.List;

public class RawTypesDemo {
    public static void main(String[] args) {
        List raw = new ArrayList();
        raw.add("Java");
        raw.add(21);

        Object first = raw.get(0);
        System.out.println(first);

        List<String> strings = raw;
        try {
            String second = strings.get(1);
            System.out.println(second);
        } catch (ClassCastException ex) {
            System.out.println("ClassCastException");
        }
    }
}

Result

Java
ClassCastException

The raw list accepts `"Java"` and `21` boxed as an `Integer`. The program prints `Java`, then catches `ClassCastException` when reading the second element as a `String` and prints the exception name.

Related lessons

  • Java · Lesson 36 · Intermediate · Version: 2

Sources