-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_generics1.java
More file actions
33 lines (26 loc) · 867 Bytes
/
java_generics1.java
File metadata and controls
33 lines (26 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//JavaGenerics
//Generics are a feature of Java that allows you to create classes and methods that work with different types of data.
//Generics are a way to make your code more flexible and reusable.
class Pair<A, B> {
A first;
B second;
Pair(A first, B second) {
this.first = first;
this.second = second;
}
A getFirst() {
return first;
}
B getSecond() {
return second;
}
public static void main(String[] args) {
Pair<Integer, String> p1 = new Pair<Integer, String>(1, "apple");
Pair<Integer, String> p2 = new Pair<Integer, String>(2, "pear");
System.out.println(p1.getFirst());
System.out.println(p1.getSecond());
System.out.println(p2.getFirst());
System.out.println(p2.getSecond());
}
}
// Java Generics