Sub topic 1
Why Generics Exist
Before Java 5, collections like List could hold any kind of object. That sounds flexible, but it caused bugs that appeared only while the program was running. Generics fix this. A generic is a class, interface, or method that takes a type as a parameter, so you can say "this is a list of strings" and the compiler will hold you to it.
The Problem: Raw Types and Casts
A raw type is a generic class used without saying what type it holds, such as plain List. A raw list stores everything as Object, the parent of all Java classes.
When you take an item out, you must cast it. A cast is an instruction that tells Java "treat this object as type X." If you are wrong, Java throws a ClassCastException at runtime.
import java.util.ArrayList;
import java.util.List;
public class RawListDemo {
public static void main(String[] args) {
List list = new ArrayList(); // raw type
list.add("hello");
list.add(42); // compiles: anything can go in
String first = (String) list.get(0); // cast required
String second = (String) list.get(1); // ClassCastException at runtime
}
}This code compiles. It crashes only when it runs, because 42 is an Integer, not a String. In a large program, the line that adds the wrong item may be far from the line that crashes, so the bug is hard to find.
The Fix: Tell the Compiler the Type
With generics, you put the element type in angle brackets. List<String> means "a list that holds only strings."
import java.util.ArrayList;
import java.util.List;
public class GenericListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("hello");
// list.add(42); // compile error: int cannot be converted to String
String first = list.get(0); // no cast needed
System.out.println(first);
}
}Now the mistake is caught at compile time, the moment you build the program. You also no longer need casts, so the code is shorter and easier to read.
What Generics Give You
| Without generics | With generics |
|---|---|
| Any object can go in | Only the declared type can go in |
| Casts needed when reading | No casts needed |
| Errors appear at runtime | Errors appear at compile time |
| Code does not say what it holds | `List<String>` documents itself |
The next chapters show how to write your own generic classes and methods, not just use the built-in ones.
Exercise
This code compiles. What happens when it runs, and how would generics prevent it? List names = new ArrayList(); names.add(3.14); String n = (String) names.get(0);
Show answer
It throws a ClassCastException at runtime, because 3.14 is a Double, not a String. Declaring the list as List<String> names = new ArrayList<>(); makes names.add(3.14) a compile error, and names.get(0) returns a String with no cast.
Exercise
Name two benefits of using List<String> instead of a raw List.
Show answer
The compiler rejects items of the wrong type, and you do not need casts when reading items. A third benefit is that the declared type documents what the list holds.