23rd: Please do not use the original ecological type in the new code

Source: Internet
Author: User

A class or interface in a declaration that has one or more type parameters (type parameter), which is a generic type.

Generic classes and generic interfaces are collectively known as generics (generic type).

Each generic defines a set of parameterized types, consisting of the name of the class or interface, followed by an angle bracket (<>) to enclose the actual type parameter list corresponding to the generic form-type parameter.

The original ecological type is like removing all generic information from a type declaration. In fact, the original eco-type list is exactly the same as the interface type list before the Java platform has no generics.

If you do not provide a type parameter, it is still legal to use the original ecological type of the collection type and other generics, but you should not do so. If you use the original ecological type, you lose

All the benefits of generics in terms of security and presentation. Since the original ecological type should not be used, why is Java allowed to use the original ecological type? Because this is for compatibility, because

Before JDK1.5, Java had entered its second decade, and before that, Java had no generics, and for the sake of the previous code, it still worked, prompting Java to still support the original ecosystem

Type.

Although primitive types such as list should not be used in new code, it is still possible to use the parameterized object type to allow the insertion of arbitrary objects, such as List<object>

The difference between the original ecological list type and the list<object> type: Not strictly speaking, the former escapes the generic check, while the latter explicitly tells the compiler that it can hold any type of object.

You can assign an object of type list<string> to a variable of the original list type, but you cannot assign an object of type list<string> to a variable of type list<object>. Because generics

Has its own subtype of the rule,list<string> is a subtype of the original ecological List, not a subtype of the parameterized type list<object>. So using a primitive ecosystem like List

Type, you lose type safety, but if you use a parameterized type such as list<object>, you will not.

23rd: Please do not use the original ecological type in the new code

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.