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

Generic Pair Class

easyGenerics, Classes

📄 Code

Read carefully — what does this print?
1class Pair<K, V> {
2 private K first;
3 private V second;
4 
5 public Pair(K first, V second) {
6 this.first = first;
7 this.second = second;
8 }
9 
10 public K getFirst() { return first; }
11 public V getSecond() { return second; }
12}
13 
14public class Main {
15 public static void main(String[] args) {
16 Pair<String, Integer> p = new Pair<>("age", 25);
17 System.out.println(p.getFirst());
18 System.out.println(p.getSecond());
19 }
20}

🎯 Your Answer