Generics
Java generics end to end: parameterized types, bounds, wildcards, erasure and everything erasure makes impossible. Interviewers use generics to separate people who consume generic APIs from people who can design them.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- Generic Fundamentals15 questions
- Generic Classes and Interfaces5 questions
- Generic Methods and Constructors5 questions
- Type Parameter Conventions and Diamond5 questions
- Bounded Type Parameters16 questions
- Upper Bounds6 questions
- Multiple Bounds5 questions
- Recursive Type Bounds5 questions
- Wildcards25 questions
- Unbounded Wildcard5 questions
- Upper-Bounded Wildcard5 questions
- Lower-Bounded Wildcard5 questions
- PECS Principle5 questions
- Wildcard Capture and Read/Write Restrictions5 questions
- Type Erasure29 questions
- Erasure Mechanism5 questions
- Bridge Methods4 questions
- Raw Types5 questions
- Reifiable vs Non-Reifiable Types5 questions
- Heap Pollution and @SafeVarargs5 questions
- Generic Signatures and Reflection5 questions
- Generic Restrictions32 questions
- No Primitive Type Arguments5 questions
- Cannot Instantiate or Array Type Parameters5 questions
- No instanceof with Parameterized Types5 questions
- No Static Members of Type Parameter5 questions
- Generics and Exceptions4 questions
- Overload Erasure Clash4 questions
- Type Tokens and Super Type Tokens4 questions
questions
117 · 5 sectionsWhat is a generic class in Java, and how do you declare one with a type parameter?
basics
~20 sA generic class is a class that takes a type as a parameter, written in angle brackets after the class name, like class Box<T>. You then use T as a placeholder type inside the class for fields and methods. When you create an object you supply a real type, e.g. Box<String>.
How do you declare a generic method in Java, and where does the type-parameter section go?
basics
~20 sYou put the type parameters in angle brackets right before the return type, e.g. static <T> T pick(T a, T b). That <T> introduces a name you can then use for the parameters and return type.
What is the diamond operator (<>) introduced in Java 7, and how does it work?
basics
~20 sThe diamond operator is the empty angle brackets <> used on the right side of an assignment. It tells the compiler to infer the generic type arguments from the left side, so you don't repeat them. Example: List<String> list = new ArrayList<>();
What is type-argument inference at a call site, and when do you need an explicit type witness?
basics
~20 sThe compiler usually figures out the type from the arguments, so you just call the method normally. When it can't (e.g. no arguments to look at), you write the type explicitly before the method name, like Collections.<String>emptyList().
What are the conventional single-letter names for Java generic type parameters (T, E, K, V, N), and why do these conventions matter?
basics
~20 sThey are agreed-on short names for generic type parameters: T means a general Type, E an Element (collections), K a Key and V a Value (maps), N a Number. They are just naming conventions that make code easier to read.
What is the syntax for declaring a type parameter with multiple bounds in Java, and what does it mean?
basics
~20 sYou write <T extends A & B & C>. It means T must be a subtype of all of A, B, and C at once, so you can call methods from all of them on a T value.
What is an upper bound on a generic type parameter in Java, and why would you use one?
basics
~20 sAn upper bound uses the extends keyword to say a type parameter must be a given type or a subtype of it, like <T extends Number>. This lets you call that type's methods inside the class or method.
In a multiple-bound declaration, what are the rules about including a class bound versus interface bounds, and their ordering?
basics
~20 sYou may have at most one class in the bounds, and if there is one it must come first. All the other bounds must be interfaces, and they go after the class. Multiple classes are not allowed.
What does the type bound <T extends Comparable<T>> mean, and why is it written that way?
basics
~20 sIt says T must be a type that can compare itself to other Ts. The bound refers back to T itself, so a value of type T can only be compared with another T, not with unrelated types.
What is the difference between a bounded type parameter <T extends Number> and an upper-bounded wildcard <? extends Number>, and when would you choose each?
basics
~20 sA bounded type parameter <T extends Number> names a type you can reuse across the signature and return. A wildcard <? extends Number> is an anonymous unknown subtype used for a single parameter; you can read Numbers from it but generally cannot add to it.
Contrast `<? extends T>` and `<? super T>`: what each allows for reading and writing, and how you decide which to use.
basics
~20 s<? extends T> is for reading: you get T's out but can't add. <? super T> is for writing: you can add T's but reads come back only as Object. Read from it → extends; write into it → super.
What does the upper-bounded wildcard `List<? extends Number>` mean, and what kinds of lists can you assign to it?
basics
~10 sList<? extends Number> means a list of some unknown type that is Number or a subtype of Number. You can assign a List<Number>, List<Integer>, or List<Double> to it.
Why can you read from a List<? extends Number> but not add elements to it (other than null), and what is the inverse rule for List<? super Integer>?
basics
~20 sWith '? extends Number' the exact type is unknown, so the compiler can't be sure a value you add is allowed, but every element is at least a Number so reading is safe. With '? super Integer' the reverse holds: you can add Integers, but reads only give you Object.
What does the wildcard `<? super T>` mean in Java generics, and what can you do with such a collection?
basics
~20 s<? super T> means "T or any of its parent types." You can safely add T (or its subtypes) to such a collection, but when you read from it you only get back Object, because you do not know the exact element type.
What does the PECS mnemonic stand for, and how do you decide between `? extends T` and `? super T`?
basics
~10 sPECS means Producer Extends, Consumer Super. If a structure gives you (produces) values, use ? extends T. If it receives the values you put in (consumes them), use ? super T.
What is type erasure in Java generics, and what does the compiler do to the type parameters?
basics
~20 sType erasure means the generic type information (like the String in List<String>) is removed during compilation. At runtime, a List<String> and a List<Integer> are both just List. The compiler uses the types only for checking and then drops them.
What is a raw type in Java, and why does the language even allow you to write `List` without type arguments?
basics
~10 sA raw type is a generic class or interface used without its type arguments, like writing List instead of List<String>. Java allows it mainly so old pre-generics code (before Java 5) still compiles.
What is type erasure in Java generics, and what happens to generic type information when code is compiled to bytecode?
basics
~20 sType erasure means the compiler removes generic type arguments after checking them, so at runtime a List<String> is just a List. The type parameters are replaced with their bounds (usually Object), and casts are inserted automatically.
What concrete operations does type erasure forbid or complicate, and what are the standard workarounds?
basics
~20 sBecause generic types are gone at runtime you cannot do new T[], T.class, or instanceof List<String>, and you cannot overload methods that differ only by type argument. Workarounds: pass a Class<T> token, use reflective array creation, or use a super type token.
What does it mean for a type to be 'reifiable' in Java, and why does the concept exist?
basics
~20 sA reifiable type is one whose full type information is still available at runtime. Because Java generics are erased (forgotten) at compile time, types like List<String> are NOT reifiable, while String, int, and List (the raw type) are.
Why can't you write List<int> in Java, and what must you use instead?
basics
~10 sGeneric type arguments must be objects, and int is a primitive, not an object. So List<int> is illegal. Use the wrapper class instead: List<Integer>. Java then converts int to Integer for you automatically.
Why can't a generic class declare a static field whose type is its own type parameter, e.g. `static T value;` in `class Box<T>`?
basics
~20 sBecause a type parameter like T belongs to each object separately, but a static field is shared by the whole class. The class can't agree on one T for all objects, so Java forbids it.
Why does declaring both method(List<String>) and method(List<Integer>) in the same class fail to compile?
basics
~10 sJava removes the generic part at compile time, so both methods become method(List). That leaves two methods with the same name and same parameter types, which is not allowed.
Why can't you write `new T()` inside a generic class or method in Java, and what is the standard workaround?
basics
~20 sJava erases the generic type T at compile time, so at runtime the program doesn't know which class T is and can't call its constructor. The usual fix is to pass a Class<T> object and call clazz.getDeclaredConstructor().newInstance(), or pass a factory/supplier that creates the object for you.
Why does Java reject `obj instanceof List<String>` at compile time, and what is the only generic form of `instanceof` it does allow?
basics
~20 sJava erases generic type info at compile time, so at runtime there is no String type stored inside the list to check against. You can only write obj instanceof List<?> (the unbounded wildcard) or obj instanceof List (raw).