Generics
Generics add a type parameter so one class works for many types safely.
Example
public class Main {
static class Box<T> { T v; Box(T v) { this.v = v; } T get() { return v; } }
public static void main(String[] args) {
Box<String> b = new Box<>("hi");
Box<Integer> n = new Box<>(5);
System.out.println(b.get() + " " + n.get());
}
}Result
hi 5
Exercise
Type parameter.
class Box<> {}
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.