Discussion
The program is run with java -ea Trial105 - what will be the output?
import java.util.*;
public class Trial105
{
public static void main(String[] args)
{
List<String> a = new ArrayList<String>();
List<Integer> b = new ArrayList<Integer>();
assert a.getClass() != b.getClass();
System.out.println(a.getClass() == b.getClass());
}
}
Answer: A
Generic type arguments are erased at compile time, so both objects are plain ArrayLists at runtime and report the same class. The assertion claiming the classes differ is false and throws. This is why a generic type cannot be recovered from an object with getClass.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development