Effective Java reading notes of four generic type

Source: Internet
Author: User

The essence of generics is the parameterized type. Valid only for compilers.

First, please do not use the original ecological type in the new code

1, generic classes and interfaces collectively known as generics, there is a corresponding primitive ecological type.

2. The existence of native types is for porting compatibility.

3. The difference between unrestricted and original ecological types is that the wildcard type is safe and the original ecological type is unsafe. You can put any element into the collection of the original ecological type, but you cannot put any element other than null into the collection<?>.

4, two exceptions:

A, only the original ecological type can be used in class, because generic information may be erased at run time.

b, the use of parameterized type is illegal when operating instanceof.

5. Summary:set<object> is a parameterized type that can contain a collection of any object type;set<?> is a wildcard type that can contain only a collection of an unknown object; Set is an original ecological type.

Ii. elimination of non-inspected warnings

1, as far as possible to eliminate every non-inspected warning.

2. You can use @suppresswarnings to eliminate the type warning of security, as it is used in the smallest possible range. Adding variable declarations is a good trick.

3. When using @suppresswarnings annotations, add a comment explaining why it is type-safe to do so.

Third, the list takes precedence over the array

The two different points of an array compared to a generic:

A, the array is covariant: Sub is a subclass of Super, sub[] is a subclass of super[].

b, arrays are materialized: they are known and checked for element type constraints at run time.

Based on the above two points: arrays and generics are not very well mixed to use. If there is an error or warning in mixed use, you should use the list instead of the array.

Iv. Preferential consideration of generics

1, using generics is more secure and easier than using types that need to be converted in client-side code.

V. Prioritize generic methods

1, static tool method is especially suitable for generics.

2. Notable features: You do not need to specify the value of the type parameter significantly, you must specify it when invoking the generic constructor.

Vi. the flexibility of using the Fraiti API with restricted pass distribution

1. The flexibility of the API can be enhanced by using a restricted wildcard type.

2, can refer to the production and consumption attributes to select extends and super. Producer-extends,comsumer-super (PECS) principle.

3, the type parameter and the wildcard character have the duality.

Vii. prioritizing type-safe heterogeneous containers

1. Heterogeneous containers are implemented by using type tokens as parameters.

Effective Java reading notes of four generic type

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.