The role and benefits of Java generics

Source: Internet
Author: User
Reproduced in: 52092692

Before the interview encountered the interviewer asked the role of generics, just said about their own understanding,

In this reprint record, deepen the impression

Generic Benefits:

Generics Simple to use

The primary goal of type-safe generics is to implement type safety for Java. Generics enable the compiler to know what the qualified type of an object is, so that the compiler can verify the type at a high level

Elimination of forced type conversions makes the code more readable and reduces the chance of errors

The benefits of introducing generics into the Java language are simple and secure. The benefit of generics is that it checks for type safety at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code.

The implementation principle of generic type

The implementation of generics is by type Erase technology type erase is done at compile time, that is, the compiler will erase the generic type parameters into its qualified type, and if not, then force the type to convert to the corresponding type when it is obtained. There is no information for generics during the run, and therefore no optimizations.

Generics do not consider inheritance

Can the list type be assigned a value of list type?
The answer is no.
Although the string type can be used as object type after assignment, the problem still occurs
Suppose the following code

The role and benefits of Java generics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.