Practice mode Java

What is the output of the Java code System.out.println(new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass());?

Correct answer C. true

Explanation

Generic type arguments are erased during compilation, so at run time there is only one class, ArrayList, for every parameterisation. Both getClass calls return that same Class object and the comparison is true. This erasure also explains why code cannot test obj instanceof List against an arbitrary Object at run time.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.