What's the difference between a type parameter and a type argument?
A type parameter is the placeholder you declare in a generic class, interface, or method (e.g., <T>). A type argument is the concrete type you provide when you use that generic (e.g., ArrayList<String> — String is the type argument).
How do you declare a simple generic class in Java?
Declare the class with a type parameter: for example, class Box<T> { private T item; public void setItem(T item){ this.item = item; } public T getItem(){ return item; } }
When should I use multiple type parameters?
Use multiple type parameters when a class or method needs to handle two or more independent types, e.g., class Product<T, U> where T is the item type and U is the price type.
Why can't I use primitive types directly as type arguments?
Generics work with reference types, not primitives. Use wrapper classes (Integer, Double, Boolean) as type arguments for primitive values.
What does the diamond operator (<>) do?
The diamond operator lets the compiler infer the type arguments on the right-hand side of an assignment, so you can write new ArrayList<>() instead of repeating the type.