Introduction to Generics

In this lesson, an explanation is provided to get started with Generics Methods (functions) in Java.

🔍 Definition

"Generics allow the reusability of code, where one single method can be used for different data-types of variables or objects."

The idea is to allow different types like Integer, String, … etc and user-defined types to be a parameter to methods, classes, and interfaces.

For example, classes like HashSet, ArrayList, HashMap, etc use generics very well. We can use them for any type.

The following example illustrates three non-generic (type-sensitive) functions for finding maximum out of 3 inputs:

Get hands-on with 1200+ tech skills courses.