Generics in Java
Generics allow you to write code that is more flexible and reusable. They allow you to create classes and methods that can work with any data type, rather than being tied to a specific type.
Example code:
csharp
public class Box<T> {
private T value;
public Box(T value) {
this.value = value;
}
public T getValue() {
return value;
}
}
Box<String> box1 = new Box<>("hello");
Box<Integer> box2 = new Box<>(42);
No comments:
Post a Comment