CJCoding With Joseph
15per day
← Back to Question List
Java Code Walkthrough #42

Bounded Generic Class with Number Methods

mediumGenerics, Classes

📄 Code

Read carefully — what does this print?
1class NumberBox<T extends Number> {
2 private T value;
3 
4 public NumberBox(T value) {
5 this.value = value;
6 }
7 
8 public double doubleValue() {
9 return value.doubleValue();
10 }
11}
12 
13public class Main {
14 public static void main(String[] args) {
15 NumberBox<Integer> intBox = new NumberBox<>(5);
16 NumberBox<Double> dblBox = new NumberBox<>(3.14);
17 
18 System.out.println(intBox.doubleValue());
19 System.out.println(dblBox.doubleValue());
20 }
21}

🎯 Your Answer