Java generics were introduced in Java 5 (many years ago) to provide functionality to the Java language to allow methods or types to operate on objects of various types, yet providing compile time safety. A classic example used all the time today is the Java collections framework.
Why Use Generics?
In a nutshell Generics offer:
- Stronger type checks at compile time.
- Eliminates excessive casting required pre Java 5.
- Allows the capability of building generic algorithms that can operate on a variety of types.
A full explanation can be found here: https://docs.oracle.com/javase/tutorial/java/generics/why.html
A Simple Java Generics Implementation
Here is an extremely simple implementation of a generic method which illustrates an algorithm that operates on a variety of types: