Generics in Java with Example: A Complete Guide

Robert Jul 13, 2026

Generics in Java are a powerful feature that enable type parameters, allowing for more reusable and type-safe code. Introduced in Java 5.0, generics bring significant advantages such as type safety, elimination of casting, and improved API design. Let's delve into the world of Java generics, exploring their syntax, benefits, and usage with examples.

Java Generics Explained
Java Generics Explained

Understanding the basics of generics is crucial before we dive into more complex topics. In simple terms, generics in Java allow us to create parameters that are type-checked against a supplied type argument when the code is compiled. This concept is a cornerstone of generic programming, enabling us to create robust and flexible code.

Generic in Java | Features, Example
Generic in Java | Features, Example

Syntax and Basic Concepts

To grasp the syntax of generics, let's start with the declaration and definition of a generic class. The generic class syntax involves using angle brackets (< >) after the class name, followed by the type parameter's name. Here's an example:

a computer screen with the words comparing list, list, and list in front of it
a computer screen with the words comparing list, list, and list in front of it

GenericClass<T> where T is the type parameter. This notation signifies that the class 'GenericClass' accepts a type parameter 'T'. T, which stands for 'Type', is a convention but can be any identifier. When creating an instance of 'GenericClass', you will specify the type argument, like this:

GenericClass<Integer> gc = new GenericClass<Integer>();

Type Parameters

Java Generics - An Exclusive Guide on Java Generics
Java Generics - An Exclusive Guide on Java Generics

Type parameters in Java generics can be bounded, enabling us to restrict the valid types that can be substituted for the type parameter. This is achieved using the 'extends' keyword. Let's consider the following example:

public static <T extends Number> void print(T t) { System.out.println(t); } print(123); // OK print("abc"); // Error: Cannot infer type argument(s) for print(T)

In this case, T is constrained to any subtype of 'Number', effectively disabling passing of string or other incompatible types.

Java generics
Java generics

Wildcards & Bounded Wildcards

Wildcards (?) in Java generics allow us to specify that a method accepts objects of any type. They're useful when working with collections or when you don't care about the specific type. However, bounded wildcards (? extends Class<T>) let us place an upper limit on the acceptable types:

Example:

GENERICS IN JAVA
GENERICS IN JAVA

public static void addStrings(List<? extends String> strings, String newString) { strings.add(newString); }

This method accepts a list of strings or any subtype of String. However, it doesn't accept a list of integers or objects due to the bounded wildcard.

Generic Interfaces and Methods

Java Generics Made Easy: An Introduction for Beginners | Generics in Java | Java Tutorial
Java Generics Made Easy: An Introduction for Beginners | Generics in Java | Java Tutorial
Packages in Java Explained | Built-in & User Defined Packages
Packages in Java Explained | Built-in & User Defined Packages
Java Tutorial: Generics in java | Java Generics [Generic types]
Java Tutorial: Generics in java | Java Generics [Generic types]
How to implement linked list data structure in Java using Generics? Example
How to implement linked list data structure in Java using Generics? Example
Java Type Conversion & Type Casting Notes
Java Type Conversion & Type Casting Notes
Complete Generics Tutorial with Java Code Examples
Complete Generics Tutorial with Java Code Examples
Basic GUI in Java | Swing GUI Tutorial for Beginners
Basic GUI in Java | Swing GUI Tutorial for Beginners
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained in 60 Seconds | Java Programming Basics
Java Variables Explained in 60 Seconds | Java Programming Basics
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
Difference between List <?>  and List<Object> in Java Generics - Example
Difference between List <?> and List<Object> in Java Generics - Example
Inheritance in Java Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Polymorphism in Java Explained | Method Overloading & Overriding Easy Notes
Wrapper Classes in Java | Autoboxing & Unboxing Explained
Wrapper Classes in Java | Autoboxing & Unboxing Explained
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Set Interface in Java
Set Interface in Java
Java Tutorial: Generics in java | Java Generics [Unbounded wildcard in Java Generics]
Java Tutorial: Generics in java | Java Generics [Unbounded wildcard in Java Generics]
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Basic Data Types in Java with Example | Java Primitive and Non-Primitive Data Types with Syntax - BTech Geeks
Basic Data Types in Java with Example | Java Primitive and Non-Primitive Data Types with Syntax - BTech Geeks
The Easiest Way To Learn Java Array Of Generic Classes
The Easiest Way To Learn Java Array Of Generic Classes

Java generics also enable the creation of generic interfaces and methods. Here's an example of a generic interface:

public interface GenericInterface<T> { T method(T t); }

In this case, the interface 'GenericInterface' accepts a type parameter 'T' and defines a method 'method' that accepts and returns an instance of 'T'.

Generic Class Method

Generic methods can also be defined outside of a class or interface, using the same syntax as before. Here's an example:

public static <T> T max(T... array) { T maxVal = array[0]; for (T value : array) { if (compare(value, maxVal) > 0) { maxVal = value; } } return maxVal; }

This example defines a generic method 'max' that accepts an array of type 'T' and returns the maximum value in the array.

In conclusion, generics in Java unlock a world of possibilities, providing numerous benefits such as improved type safety, enhanced API design, and increased code maintainability. Embracing generics in your Java coding journey can significantly improve the quality and readability of your code.