skip to content

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 pageshow

explore

questions

117 · 5 sections

What is a generic class in Java, and how do you declare one with a type parameter?

level: juniorimportance: must knowfreq 85%
basics
~20 s

A 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>.

open as a page

How do you declare a generic method in Java, and where does the type-parameter section go?

level: juniorimportance: must knowfreq 78%
basics
~20 s

You 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.

open as a page

What is the diamond operator (<>) introduced in Java 7, and how does it work?

level: juniorimportance: must knowfreq 70%
basics
~20 s

The 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<>();

open as a page

What is type-argument inference at a call site, and when do you need an explicit type witness?

level: middleimportance: must knowfreq 62%
basics
~20 s

The 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().

open as a page

What are the conventional single-letter names for Java generic type parameters (T, E, K, V, N), and why do these conventions matter?

level: juniorimportance: should knowfreq 55%
basics
~20 s

They 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.

open as a page

What is the syntax for declaring a type parameter with multiple bounds in Java, and what does it mean?

level: juniorimportance: must knowfreq 55%
basics
~20 s

You 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.

open as a page

What is an upper bound on a generic type parameter in Java, and why would you use one?

level: juniorimportance: must knowfreq 70%
basics
~20 s

An 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.

open as a page

In a multiple-bound declaration, what are the rules about including a class bound versus interface bounds, and their ordering?

level: middleimportance: must knowfreq 48%
basics
~20 s

You 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.

open as a page

What does the type bound <T extends Comparable<T>> mean, and why is it written that way?

level: middleimportance: must knowfreq 55%
basics
~20 s

It 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.

open as a page

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?

level: seniorimportance: must knowfreq 55%
basics
~20 s

A 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.

open as a page

Contrast `<? extends T>` and `<? super T>`: what each allows for reading and writing, and how you decide which to use.

level: juniorimportance: must knowfreq 66%
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.

open as a page

What does the upper-bounded wildcard `List<? extends Number>` mean, and what kinds of lists can you assign to it?

level: juniorimportance: must knowfreq 70%
basics
~10 s

List<? 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.

open as a page

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>?

level: middleimportance: must knowfreq 70%
basics
~20 s

With '? 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.

open as a page

What does the wildcard `<? super T>` mean in Java generics, and what can you do with such a collection?

level: middleimportance: must knowfreq 70%
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.

open as a page

What does the PECS mnemonic stand for, and how do you decide between `? extends T` and `? super T`?

level: middleimportance: must knowfreq 72%
basics
~10 s

PECS 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.

open as a page

What is type erasure in Java generics, and what does the compiler do to the type parameters?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Type 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.

open as a page

What is a raw type in Java, and why does the language even allow you to write `List` without type arguments?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A 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.

open as a page

What is type erasure in Java generics, and what happens to generic type information when code is compiled to bytecode?

level: juniorimportance: must knowfreq 78%
basics
~20 s

Type 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.

open as a page

What concrete operations does type erasure forbid or complicate, and what are the standard workarounds?

level: middleimportance: must knowfreq 70%
basics
~20 s

Because 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.

open as a page

What does it mean for a type to be 'reifiable' in Java, and why does the concept exist?

level: middleimportance: must knowfreq 55%
basics
~20 s

A 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.

open as a page

Why can't you write List<int> in Java, and what must you use instead?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Generic 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.

open as a page

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>`?

level: juniorimportance: must knowfreq 55%
basics
~20 s

Because 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.

open as a page

Why does declaring both method(List<String>) and method(List<Integer>) in the same class fail to compile?

level: juniorimportance: must knowfreq 62%
basics
~10 s

Java 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.

open as a page

Why can't you write `new T()` inside a generic class or method in Java, and what is the standard workaround?

level: middleimportance: must knowfreq 70%
basics
~20 s

Java 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.

open as a page

Why does Java reject `obj instanceof List<String>` at compile time, and what is the only generic form of `instanceof` it does allow?

level: middleimportance: must knowfreq 62%
basics
~20 s

Java 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).

open as a page