Skip to content

Commit 577df8b

Browse files
committed
Erasure and Raw type compatibility
1 parent b1168dd commit 577df8b

5 files changed

Lines changed: 74 additions & 41 deletions

File tree

src/main/java/com/ab/generics/erasure/Erasure.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,38 @@
44

55
/**
66
* @author Arpit Bhardwaj
7+
*
8+
* Compiler earses information about generics from compiled code.
9+
* compiler verifies type safety of your code before erasing generics
10+
* compiler add relevant typecasting operations
711
*/
8-
public class Erasure<T,B extends Comparable<B>> {
9-
public void unBounded(T param){
1012

13+
//original source code
14+
public class Erasure<T> {
15+
public T apply(T t){
16+
return t;
1117
}
12-
public void lists(List<String> param1, List<List<T>> param2){
13-
18+
public static void main(String[] args) {
19+
Erasure<String> e = new Erasure<>();
20+
String apply = e.apply("");
1421
}
15-
public void bounded(B param){
22+
}
23+
24+
/*
25+
//Compiled Code
1626
27+
public class Erasure {
28+
public Object apply(Object t){
29+
return t;
30+
}
31+
public static void main(String[] args) {
32+
Erasure<String> e = new Erasure<>();
33+
String apply = (String) e.apply("");
1734
}
1835
}
36+
*/
37+
38+
39+
40+
41+

src/main/java/com/ab/generics/erasure/Legacy.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@
33
import java.util.ArrayList;
44
import java.util.Iterator;
55
import java.util.List;
6+
import java.util.function.UnaryOperator;
67

78
/**
89
* @author Arpit Bhardwaj
910
*/
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<>();
11+
//original source code
12+
public class Legacy implements UnaryOperator<String> {
13+
@Override
14+
public String apply(String s) {
15+
return s;
16+
}
1517

16-
list.add("abc");
17-
list.add(1);
18-
list.add(new Object());
18+
public static void main(String[] args) {
19+
Legacy e = new Legacy();
20+
String apply = e.apply("");
21+
}
22+
}
1923

20-
List<Integer> integers = new ArrayList();
21-
integers = list;
22-
List<String> strings = new ArrayList();
23-
list = strings;
24+
/*
25+
//Compiled Code
2426
25-
Iterator iterator = list.iterator();
26-
while (iterator.hasNext()){
27-
System.out.println(iterator.next());
28-
}
27+
public class Legacy implements UnaryOperator<String> {
28+
@Override
29+
public String apply(final String s) {
30+
return s;
31+
}
2932
30-
//Leads to ClassCastException
31-
for (Integer elem:integers) {
32-
System.out.println(elem);
33-
}
33+
//bridge synthetic method generated by compiler
34+
public Object apply(final Object o){
35+
return this.apply((String o));
3436
}
37+
3538
}
39+
*/

src/main/java/com/ab/generics/reflection/ReifiableTypes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/**
77
* Types that are reifiable can be reflected
88
* Primitives
9-
* Non Parameterized Class/inteface
10-
* All Type arguments unbounded wildcards
9+
* Non parameterized class/interface
10+
* All type arguments unbounded wildcards
1111
* Raw Types
1212
* Arrays
1313
*

src/main/java/com/ab/generics/wildcard/CovariantArrays.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
/**
99
* @author Arpit Bhardwaj
1010
*
11-
* Arrays Are Covariant in java means
12-
*
13-
* an array of type T[] may contain elements of type T or any subtype of T.
14-
* an array S[] is a subtype of the array T[] if S is a subtype of T
11+
* Arrays are covariant which result in runtime exceptions (ArrayStoreException)
12+
* an array of type T[] may contain elements of type T or any subtype of T.
13+
* an array S[] is a subtype of the array T[] if S is a subtype of T
1514
*
1615
* Arrays are of Refiable Type
16+
*
17+
* All Collection API uses generics that are invariant; code validate at compile time
1718
*/
1819
public class CovariantArrays {
1920
public static void main(String[] args) {
@@ -32,10 +33,18 @@ public static void main(String[] args) {
3233

3334
System.out.println(Arrays.toString(numbers2));
3435

36+
3537
List<Integer> integers3 = new ArrayList<>();
3638
integers3.add(1);
3739
integers3.add(2);
38-
//List<Number> myNums = integers; //compiler error as generic types are non-reifiable
40+
//List<Number> myNums = integers; //compiler error as generic types are invariant and non-reifiable
41+
42+
//we can bypass the generics compiler check in collection using raw types, which can result in runtime exception
43+
List myInts = integers3;
44+
List<Number> myNums = myInts;
45+
myNums.add(12L);
46+
Integer integer = integers3.get(2);//throws java.lang.ClassCastException
47+
System.out.println(integer);
3948

4049
//due to above behavior of generics in collection we are losing the power of polymorphism
4150
//In order to overcome above limitation and support polymorphism in generic world
@@ -44,15 +53,14 @@ public static void main(String[] args) {
4453
//contra variance (upper bound) (read, but not write.)
4554
//number3 accepts elements of Number Type or any subclasses of Number
4655
List<? extends Number> numbers3 = new ArrayList<Integer>();
47-
Number n1 = numbers3.get(0);
48-
//numbers3.add(2);//compile error
56+
//numbers3.add(2);//compile error as it is read only
4957

5058
//covariance (lower bound) (write, but not read.)
5159
//number4 accepts elements of Integer Type or any super classes of Integer
5260
List<? super Integer> numbers4 = new ArrayList<Number>();
53-
//Number n2 = numbers4.get(0);//compile error
5461
numbers4.add(2);
5562
//numbers4.add(1.0);//compile error
63+
//Number n2 = numbers4.get(0);//compile error as it is write only
5664

5765
}
5866
}

src/main/java/com/ab/generics/wildcard/Unbounded.java renamed to src/main/java/com/ab/generics/wildcard/WildcardsDemo.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,25 @@
99
/**
1010
* @author Arpit Bhardwaj
1111
*/
12-
public class Unbounded {
12+
public class WildcardsDemo {
1313

1414
public static void main(String[] args) {
1515
//List objects = new ArrayList();
1616
//List objects = new ArrayList( );
1717
//List objects = new ArrayList<>();
1818
//List objects = new ArrayList< >( );
1919
//List objects = new ArrayList< Object >( );
20-
2120
//List<> objects = new ArrayList();//not allowed
2221

23-
//List<Object> objects = new ArrayList();
24-
List<Object> objects = new ArrayList<>();
22+
List<Object> objects = new ArrayList<>();// this is same as raw type declaration List objects = new ArrayList<>();
2523
objects.add(1);
2624
objects.add("abc");
2725
objects.add(new Object());
2826
objects.add(new Person("Nish", 28));
2927
System.out.println(objects);
3028

31-
3229
//the only value pass through ? is null
30+
//because ? represents unknown type and No Objects in java is of unknown type, so no values except null can be added to such collection
3331
List<?> objects2 = new ArrayList<>();
3432
objects2.add(null);
3533
//Compile time error
@@ -47,7 +45,7 @@ public static void main(String[] args) {
4745

4846
//var objects4 = new HashSet<?>();//not allowed
4947
//var objects4 = new HashSet<String>();//allowed
50-
var objects4 = new HashSet<>();//implicit object type inferred
48+
var objects4 = new HashSet<>();// this is same as raw type declaration HashSet objects4 = new HashSet<>();
5149
objects4.add(1);
5250
objects4.add("abc");
5351
objects4.add(new Object());

0 commit comments

Comments
 (0)