The meaning of Java generic Java using generics

Source: Internet
Author: User
The meaning of Java generic Java using generics

@author Ixenos

Direct meaning

Guaranteed type safety at compile time

Fundamental significance

A) type safety issues derive from the design of reusable codes, generics guarantee type-safe reuse templates

b) Do not manually force type conversions when using a reuse template

Advantages and disadvantages of three types of generic implementations

  • C + +: template mode implementation, fully expanded at compile time and support for partial, type precision, code sharing is poor;
  • Java 5: Erase method implementation, only for compile-time type check, wipe at run time, backward compatibility good, code sharing good, type accuracy is very poor;
  • C # 2.0: hybrid Implementation, expanded at run time, type precision, code sharing is OK.

  • The template method is to generate a specific template for each type, and a generic class will have multiple templates
  • The erase method is that all generic type information is erased at compile time, and a generic class has only one template

Q:java Why do I use type erase?

A: For backwards compatibility (referred to as backward compatibility at the binary level, not on the source)

For details, please refer to: reifiable generics and type erasure generics each have what advantages and disadvantages?

Why is Java not implementing a true generic type?

At what stage does Java generic erasure occur, and how do I use the Anti-compilation tool to see the code after the generic erase?

The meaning of Java generic Java using 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.