Mastering Java Generics: A Practical Example

Java generics, introduced in Java 5, enable type parameters in class and method declarations, allowing for more type-safe and reusable code. Let's dive into a practical example to understand how Java generics work and how they can benefit your code.

Understanding the Problem: The Non-Generic Box Example
Before we introduce generics, let's consider a simple non-generic class, `Box`, that can hold any type of object.

public class Box {
private Object object;
public void set(Object object) {
this.object = object;
}
public Object get() {
return object;
}
}
While this class seems versatile, it comes with a significant drawback: type safety. Since the `Box` can hold any type of object, you'll lose the type information at compile time, leading to potential runtime errors.
Introducing Generics: The Generic Box Example

Now, let's refactor the `Box` class to use generics, creating a type-safe and reusable `Box` class.
public class Box<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
The `
Creating a Generic Box

To create a generic `Box`, you must specify the type when instantiating the class:
Box<String> stringBox = new Box<String>();
stringBox.set("Hello, Generics!");
Using a Generic Box
Now, you can use the `Box` with the confidence that it will hold the specified type:

String s = stringBox.get();
If you try to insert a different type into the `Box`, the compiler will throw an error:
stringBox.set(123); // Error: incompatible types: int cannot be converted to String
Generic Methods


![Java Tutorial: Generics in java | Java Generics [Generic types]](https://i.pinimg.com/originals/be/4d/60/be4d60e1fa255b781265f8addec2a6ce.jpg)




![Java Tutorial: Generics in java | Java Generics [Generics Introduction]](https://i.pinimg.com/originals/3f/d6/67/3fd66753a1724ebe15080102ca9bb271.jpg)







![Java Tutorial: Generics in java | Java Generics [Generic raw type]](https://i.pinimg.com/originals/bb/7d/17/bb7d17b9dfbf2ba306bf6a4faa017508.jpg)

![Java Tutorial: Generics in java | Java Generics [The extends wildcard boundary]](https://i.pinimg.com/originals/5a/6e/16/5a6e161b21736877b97ff492ecf8b2d3.jpg)


Java generics also support generic methods. Here's an example of a generic method that swaps the values of two objects:
public static <T> void swap(T a, T b) {
T temp = a;
a = b;
b = temp;
}
To use this method, you must specify the type when calling it:
Integer a = 5, b = 10;
swap(a, b); // Now a is 10 and b is 5
Bounded Wildcards
Sometimes, you might want to create a method that accepts a type parameter with certain constraints. In such cases, you can use bounded wildcards (` extends T>` or ` super T>`). Here's an example of a method that accepts a list of `Number` or its subclasses:
public static void printNumbers(List<? extends Number> list) {
for (Number number : list) {
System.out.println(number);
}
}
This method can accept lists of `Integer`, `Double`, or any other `Number` subclass, but not lists of unrelated types like `String`.
Conclusion
Java generics empower you to write type-safe, reusable, and maintainable code. By understanding and leveraging generics, you can create more robust and expressive applications. In this article, we explored a practical example of using generics in Java, demonstrating their benefits and various use cases.