Erasure
This lesson explains how erasure works in Java.
We'll cover the following...
We'll cover the following...
1.
What is type erasure?
0/500
Show Answer
Did you find this helpful?
Press + to interact
Java
import java.util.*;class Demonstration {public static void main( String args[] ) {// Raw typeArrayList al = new ArrayList();// ArrayList of StringsArrayList<String> strs = new ArrayList<>();// ArrayList of ArrayList of StringsArrayList<ArrayList<String>> lstOfStringLsts = new ArrayList<>();System.out.println("al.getClass().equals(strs.getClass()) = " + al.getClass().equals(strs.getClass()));System.out.println("al.getClass().equals(lstOfStringLsts.getClass()) = " + al.getClass().equals(lstOfStringLsts.getClass()));}}
1.
Can you give examples of type erasure?
0/500
Show Answer
1 / 4
Press + to interact
Java
import java.util.*;class Demonstration {public static void main( String args[] ) {}<T extends List<Integer>> void print(T lst) {for (Integer i : lst)System.out.println(i);}<T extends List<String>> void print(T lst) {for (String str : lst)System.out.println(str);}<T extends List> void print(T lst) {for (Object str : lst)System.out.println(str);}}
1.
What is the difference between C++ template library and Java generics?
0/500
Show Answer
1 / 2
Press + to interact
Java
import java.util.*;class Demonstration {public static void main( String args[] ) {List<Integer> list = new ArrayList<>();someMethod(list);}/** Uncomment the following method and comment out the* other method to see the compile error generated.**//*static void someMethod(List<Number> nums){// ... method body}*/static void someMethod(List<? extends Number> nums){// ... method bodySystem.out.println("Comment out this method and uncomment the already commented method to see the compiler error.");}}
Consider the following snippet to understand why List<Integer>
is ...