public class Box <T extends Number> {
private T data;
}
Box<Integer> intBox = new Box<>(2); // Valid type argument
Box<Double> doubleBox = new Box<>(2.5); // Valid type argument
Box<String> stringBox = new Box<>("hello"); // Error
public static <T extends Number> boolean isZero(T data) {
return data.equals(0);
}
//multiple bounds
public class Box <T extends Number & Comparable<T>> {
private T data;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter