Skip to content

Commit fbfe00b

Browse files
author
Arpit Bhardwaj
committed
Erasure: Working of Generics under the hood
1 parent 63450d1 commit fbfe00b

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ab.generics.erasure;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Arpit Bhardwaj
7+
*/
8+
public class Erasure<T,B extends Comparable<B>> {
9+
public void unBounded(T param){
10+
11+
}
12+
public void lists(List<String> param1, List<List<T>> param2){
13+
14+
}
15+
public void bounded(B param){
16+
17+
}
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.ab.generics.erasure;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
/**
8+
* @author Arpit Bhardwaj
9+
*/
10+
public class Legacy {
11+
public static void main(String[] args) {
12+
List list = new ArrayList();
13+
//not same as
14+
//List<Object> list = new ArrayList<>();
15+
16+
list.add("abc");
17+
list.add(1);
18+
list.add(new Object());
19+
20+
List<Integer> integers = new ArrayList();
21+
integers = list;
22+
List<String> strings = new ArrayList();
23+
//list = strings;
24+
25+
Iterator iterator = list.iterator();
26+
while (iterator.hasNext()){
27+
System.out.println(iterator.next());
28+
}
29+
30+
//Leads to ClassCastException
31+
for (Integer elem:integers) {
32+
System.out.println(elem);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)