Generics are a facility of generic programming that were added to the Java programming language in 2004 within J2SE 5.0. They allow “a type or method to operate on objects of various types while providing compile-time type safety.” This feature specifies the type of objects stored in a Java Collection.
Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively.
Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.
Using Java Generic concept, we might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements.
Related Tags
Tutorials
PECS (Producer Extends, Consumer Super) tells us when to use extends T> and when to use super T> on a generic parameter. Learn the rule with compile errors, a copy() method and the JDK methods that follow it.
Learn about Java Generics PECS which is related to producing and consuming collections with easy-to-follow examples.
Java Generics is a technical term denoting a set of language features related to the definition and use of generic types and methods.