Mastering Java Generics

πŸ“˜ Java πŸ‘ 51 views πŸ“… Dec 01, 2025
⏱ Estimated reading time: 2 min

Generics in Java provide a way to write classes, interfaces, and methods that operate on any type of data while maintaining type safety at compile-time. They reduce runtime errors and eliminate the need for type casting.


1. Why Generics?

Before Generics:

List list = new ArrayList(); list.add("Java"); String s = (String) list.get(0); // Type casting required

Problems:

  • Type casting is error-prone

  • No compile-time type checking

Generics solve this by enforcing type safety:

List list = new ArrayList<>(); list.add("Java"); // No need for casting String s = list.get(0);

2. Generic Classes

A generic class allows you to define a class with type parameters.

class Box { private T content; void setContent(T content) { this.content = content; } T getContent() { return content; } } public class Test { public static void main(String[] args) { Box stringBox = new Box<>(); stringBox.setContent("Java Generics"); System.out.println("Content: " + stringBox.getContent()); Box intBox = new Box<>(); intBox.setContent(100); System.out.println("Content: " + intBox.getContent()); } }

Output:

Content: Java Generics Content: 100

3. Generic Methods

A generic method can operate on different types independently of the class’s type.

public class Test { public static void display(T element) { System.out.println("Element: " + element); } public static void main(String[] args) { display("Hello"); display(123); display(45.6); } }

Output:

Element: Hello Element: 123 Element: 45.6

4. Bounded Generics

  • Restrict the type parameter to a specific class or its subclasses using extends.

class Test { public static extends Number> void display(T number) { System.out.println("Number: " + number); } public static void main(String[] args) { display(100); // Integer display(45.5); // Double // display("Java"); // Error: String not allowed } }

5. Wildcards in Generics

  • ? β†’ Unknown type.

  • Upper-bounded: <? extends Number> β†’ Accepts Number and subclasses

  • Lower-bounded: <? super Integer> β†’ Accepts Integer and superclasses

List<? extends Number> list1; // List of Number or subclass List<? super Integer> list2; // List of Integer or superclass

6. Advantages of Generics

  • Type safety β†’ Compile-time error for incompatible types

  • No casting required β†’ Cleaner code

  • Code reusability β†’ Single class/method for multiple types

  • Better readability and maintainability


7. Key Points

  • Generics are used extensively in Collections Framework (ArrayList, HashMap).

  • Generics cannot be applied to primitive types directly (int β†’ use Integer).

  • Type erasure β†’ Java removes generic type info at runtime; generics exist only at compile-time.


8. Conclusion

Java Generics provide a powerful mechanism for writing type-safe, reusable, and maintainable code. Mastering generics is essential for working with collections, APIs, and custom data structures in modern Java applications.


πŸ”’ Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes