In Java programming generics allow for the creation of classes, methods, and interfaces that can work with different types while ensuring type safety at compile time. Generics provide stability to the code by making it more type-safe and easier to read. They enable programmers to express their intent and restrict collections to contain elements of a specific data type.
// Generic class to hold any type of element
class Container { private T element; public void setElement(T element) { this.element = element; } public T getElement() { return element; } } |
In this code snippet, the Container class is a generic class that can hold elements of any type. The type parameter T represents the type of the element to be stored. The setElement method sets the element, and the getElement method retrieves the element from the container.
// Example usage of the generic class public class Main { public static void main(String[] args) { // Creating an instance of Container with Integer type Container<Integer> integerContainer = new Container<>(); integerContainer.setElement(42); // Retrieving the element from the integerContainer Integer intValue = integerContainer.getElement(); System.out.println("Integer value: " + intValue); // Creating an instance of Container with String type Container<String> stringContainer = new Container<>(); stringContainer.setElement("Hello, Generics!"); // Retrieving the element from the stringContainer String strValue = stringContainer.getElement(); System.out.println("String value: " + strValue); // Creating an instance of Container with Float type Container<Float> floatContainer = new Container<>(); floatContainer.setElement(3.14f); // Retrieving the element from the floatContainer Float floatValue = floatContainer.getElement(); System.out.println("Float value: " + floatValue); } } |
Output:
Integer value: 42
String value: Hello, Generics!
Float value: 3.14
In the Main class, we demonstrate the usage of the Container class with different types.
First, we create an instance of Container<Integer> called integerContainer. This means the integerContainer object can only hold elements of type Integer. We set the element to 42 using the setElement method.
Next, we retrieve the element from integerContainer using the getElement method and assign it to an Integer variable intValue. Finally, we print the value of intValue, which will be 42.
Similarly, we create another instance of Container<String> called stringContainer. This container will hold elements of type String. We set the element to the string “Hello, Generics!” and retrieve it using getElement. The value is then printed.
Next, we create an instance of Container<Float> called floatContainer. This container will hold elements of type Float. We set the element to 3.14f (a float value) and retrieve it using getElement. Finally, the value is printed.
The benefit of using generics is that we can create a single class (Container) that can hold elements of different types without the need for separate classes for each type. The code is type-safe, and the compiler performs strong type checking, reducing the possibility of runtime errors.
Leave a Reply