Java Generics features were added to the Java language from Java 5. Generics add a way to specify particular types to general-purpose classes and methods that operated on Object before. Java generics provide compile-time type checking and removing the risk of ClassCastException that was common while working with collection classes.

Java Generics

Generics were introduced to Java by JDK 5. It changed Java in two important ways – it added a syntactical element to the language, and, it caused changes to many of the classes and methods in the core API. Generics makes it possible to create classes,  interfaces, and methods that can work with various kinds of data in a type-safe manner.

It is often found that the same algorithm needs to be implemented for different types of data. So rather than creating different classes or methods for different data types, it is convenient to create classes or methods which are generic.

#java #classcastexception #java generics

Java Generics Example | The Basics of Java Generics
2.10 GEEK