Java Programming Handbook

    Introduction to Generics in Java

    Introduction#

    Generics in Java allow us to create classes, interfaces, and methods with type parameters. This provides type safety, code reusability, and eliminates type casting. Before Generics, Java developers used objects of type Object, leading to runtime errors. Generics solve this problem by enabling compile-time type checking.

    In this blog, we will cover:

    • What are Generics?
    • Why Use Generics?
    • Advantages of Generics
    • Basic Syntax of Generics
    • Examples

    What are Generics?#

    Generics allow you to create type-safe classes and methods by defining a placeholder for types. Instead of specifying a fixed data type, you can use a type parameter that is replaced by an actual type during runtime.

    Example Without Generics (Before Java 5)#

    class DataContainer { private Object data; public void setData(Object data) { this.data = data; } public Object getData() { return data; } } public class WithoutGenerics { public static void main(String[] args) { DataContainer container = new DataContainer(); container.setData("Hello"); String str = (String) container.getData(); // Requires explicit casting System.out.println(str); } }

    Example With Generics#

    class GenericContainer<T> { private T data; public void setData(T data) { this.data = data; } public T getData() { return data; } } public class WithGenerics { public static void main(String[] args) { GenericContainer<String> container = new GenericContainer<>(); // Type-safe container.setData("Hello Generics"); String str = container.getData(); // No casting required System.out.println(str); } }

    With Generics: No type casting, better readability, and type safety.

    Why Use Generics?#

    1. Type Safety – Generics ensure that only the specified type is allowed in an instance.
    2. Code Reusability – The same class or method can be used with different data types.
    3. No Need for Type Casting – Avoids unnecessary explicit casting of objects.
    4. Compile-Time Checking – Detects type-related errors at compile-time rather than runtime.

    Advantages of Generics#

    FeatureWithout GenericsWith Generics
    Type Safety❌ No✅ Yes
    Type Casting❌ Required✅ Not Required
    Code Reusability❌ No✅ Yes
    Performance❌ Slower (due to casting)✅ Faster
    Compile-Time Checking❌ No✅ Yes

    Basic Syntax of Generics#

    Generic Class Syntax#

    class Box<T> { // T is a type parameter private T value; public void setValue(T value) { this.value = value; } public T getValue() { return value; } }

    Creating an Instance of a Generic Class#

    public class GenericExample { public static void main(String[] args) { Box<Integer> intBox = new Box<>(); intBox.setValue(100); System.out.println(intBox.getValue()); Box<String> strBox = new Box<>(); strBox.setValue("Hello Generics"); System.out.println(strBox.getValue()); } }

    Key Point: The same Box class works for both Integer and String without modification.

    Conclusion#

    In this blog, we introduced Generics in Java and covered:

    • What Generics are and why they are useful
    • How Generics improve type safety and reusability
    • The advantages of using Generics over raw types
    • The basic syntax of a Generic Class with examples

    Generics provide a powerful way to write flexible and type-safe Java code. In the next blog, we will explore Defining Generic Classes in detail with examples.

    Last updated on Apr 09, 2025