Tutorial by Examples

Generics enable classes, interfaces, and methods to take other classes and interfaces as type parameters. This example uses generic class Param to take a single type parameter T, delimited by angle brackets (<>): public class Param<T> { private T value; public T getValue() ...
Methods can also have generic type parameters. public class Example { // The type parameter T is scoped to the method // and is independent of type parameters of other methods. public <T> List<T> makeList(T t1, T t2) { List<T> result = new ArrayList<T&...
Java SE 7 Java 7 introduced the Diamond1 to remove some boiler-plate around generic class instantiation. With Java 7+ you can write: List<String> list = new LinkedList<>(); Where you had to write in previous versions, this: List<String> list = new LinkedList<String>()...
You can require a generic type to extend multiple upper bounds. Example: we want to sort a list of numbers but Number doesn't implement Comparable. public <T extends Number & Comparable<T>> void sortNumbers( List<T> n ) { Collections.sort( n ); } In this example T must...
You can restrict the valid types used in a generic class by bounding that type in the class definition. Given the following simple type hierarchy: public abstract class Animal { public abstract String getSound(); } public class Cat extends Animal { public String getSound() { ...
The syntax for Java generics bounded wildcards, representing the unknown type by ? is: ? extends T represents an upper bounded wildcard. The unknown type represents a type that must be a subtype of T, or type T itself. ? super T represents a lower bounded wildcard. The unknown type repres...
Code that uses generics has many benefits over non-generic code. Below are the main benefits Stronger type checks at compile time A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runt...
Generic parameters can also be bound to more than one type using the T extends Type1 & Type2 & ... syntax. Let's say you want to create a class whose Generic type should implement both Flushable and Closeable, you can write class ExampleClass<T extends Flushable & Closeable> { }...
Due to type erasure the following will not work: public <T> void genericMethod() { T t = new T(); // Can not instantiate the type T. } The type T is erased. Since, at runtime, the JVM does not know what T originally was, it does not know which constructor to call. Workarounds ...
How do you go about using an instance of a (possibly further) inherited generic type within a method declaration in the generic type itself being declared? This is one of the problems you will face when you dig a bit deeper into generics, but still a fairly common one. Assume we have a DataSeries&l...
Using generics to define the type in instanceof Consider the following generic class Example declared with the formal parameter <T>: class Example<T> { public boolean isTypeAString(String s) { return s instanceof T; // Compilation error, cannot use T as class type here ...
Suppose the following generic interface has been declared: public interface MyGenericInterface<T> { public void foo(T t); } Below are listed the possible ways to implement it. Non-generic class implementation with a specific type Choose a specific type to replace the formal type ...
With generics, it's possible to return whatever the caller expects: private Map<String, Object> data; public <T> T get(String key) { return (T) data.get(key); } The method will compile with a warning. The code is actually more safe than it looks because the Java runtime will d...
Many unbound generic parameters, like those used in a static method, cannot be recovered at runtime (see Other Threads on Erasure). However there is a common strategy employed for accessing the type satisfying a generic parameter on a class at runtime. This allows for generic code that depends on ac...

Page 1 of 1