Nearby lessons

81 of 125

Java - Generics

📌 What You Will Learn
  • The problem generics solve
  • Generic classes and generic methods
  • Bounded type parameters
  • Wildcards: ?, ? extends T, ? super T
  • Type erasure — how generics work inside

Generics is a core concept of the Java language. This lesson explains The Problem Generics Solve, Generic Class and Generic Method with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The Problem Generics Solve

Before Java 5, a collection stored any kind of object. That meant you could accidentally add a String into a list of numbers, and the mistake would be discovered only at run time (crash), not at compile time.

Generics (Java 5+) fix this by letting you say clearly: this list can hold only Integers. The compiler then checks at compile time and stops mistakes early.

In simple words: Generics move the type check from run time to compile time — a List<Integer> simply refuses a String, so the mistake shows up as an error in the editor instead of a crash later.
Example01
Loading editor…

The Problem Generics Solve

Benefits: type safety (mistakes found at compile time), no casting (cleaner code), and self-documenting code (you can read what the list holds).

Example02
Loading editor…

Generic Class

A generic class uses type parameters written in angle brackets <T>. T is a placeholder for the real type, decided when we use the class.

In simple words: `<T>` is just a placeholder: one generic `Box<T>` class becomes many typed versions — Box<String>, Box<Integer>, Box<Student> — depending on the type you put in the angle brackets.

One class Box, many types — Box<String>, Box<Integer>, Box<Student>. The angle brackets < > are called the diamond operator (Java 7 lets us write new Box<>() without repeating the type).

Example03
Loading editor…

Generic Method

A generic method has its own type parameter before the return type:

Example04
Loading editor…

Bounded Type Parameters

Sometimes we want T to be restricted to a specific family — like only numbers so we can add them. We use extends to set an upper bound.

This gives us access to Number's methods (like doubleValue()), which plain T would not have.

Example05
Loading editor…

Wildcards — ? , ? extends T , ? super T

Wildcards make generic methods more flexible. The ? means unknown type.

SyntaxMeaningExample use
?Any typevoid show(List<?> list) — accepts a list of anything
? extends TT or its children (upper bound)void sum(List<? extends Number> list) — accepts Integer, Double...
? super TT or its parents (lower bound)void add(List<? super Integer> list) — accepts Integer or Object lists
In simple words: `?` means any type, `? extends T` means T or its children, and `? super T` means T or its parents — wildcards let one method accept a whole family of types.
Trainer's Note: Easy memory trick: PECS — Producer Extends, Consumer Super. If a method produces (reads) values from a collection, use ? extends T. If it consumes (writes) values into it, use ? super T.
Example06
Loading editor…

Type Erasure — How Generics Really Work

Here is a secret: generics exist only at compile time. At run time, Java erases all type information (this is called type erasure). The compiled .class file works with plain Object, and the compiler adds the casts for you.

In simple words: Type erasure means generics are a compile-time-only illusion — the compiler checks your types, then wipes them away and inserts the casts, so at run time List<String> is just a plain List.

That is why list instanceof List<String> is not allowed at run time — the <String> part simply does not exist then. The generics system protects you at compile time, then quietly steps aside at run time.

Example07
Loading editor…

Common Generic Types You Already Use

Example08
Loading editor…

Why Generics with Collections — Question and Answer

The classic material links generics tightly to collections. The two reasons:

  • Type safety — without generics, a collection accepts anything, and mistakes are found only at run time. With generics, ArrayList<String> refuses a number at compile time.
  • No casting — when you read from a collection, generics remove the manual cast: String s = list.get(0) instead of String s = (String) list.get(0).

That is why modern Java code always writes List<String>, Map<String, Integer> etc. — generics make collections safe and clean.

Example09
Loading editor…
📝 Key Takeaways
  • Generics give compile-time type safety and remove manual casting.
  • Generic class: class Box<T>; generic method: static <T> void m(...).
  • Bounded types: <T extends Number> restricts T.
  • Wildcards: ? = any, ? extends T = T or children, ? super T = T or parents.
  • PECS: producers use extends, consumers use super.
  • Type erasure: generics are compile-time only; at run time they are gone.

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.