Practice mode Java

What is the main benefit of using generics in Java collections?

Correct answer C. Type errors are caught at compile time and casts are avoided

Explanation

With a declaration such as List, the compiler rejects insertion of the wrong type and inserts casts for you on retrieval. Errors surface when compiling instead of as a ClassCastException later. Generics do not remove boxing or shrink storage, because type information is largely erased and the same bytecode handles all type arguments.

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.