Effective Java experience, generics

Source: Internet
Author: User
Tags comparable

For generics this chapter, when starting to write the frame, did not pay attention to the details, resulting in many methods or the wrong design of the class. This book makes up for me a short board at this point. The approximate summary is as follows.

1. do not use the original ecological type in the new code . In fact, the list does not bring anything to the original ecological type. The origin of the original ecological type is to solve the migration compatibility. If you want to write secure code, use generics. This allows you to indicate whether you are using it correctly at compile time. Otherwise, the original ecological type will have an exception that cannot be converted by the class at run time.

2. eliminate non-inspected warnings. well-formed and safe code should have no warnings or minimize warnings. If you can determine a non-inspected warning, you can dismiss the warning by annotating @suppresswarnings ("unchecked") in front of it. and write a comment next to why the warning is annotated. Easy for others to read the code.

3. the list takes precedence over the array. arrays and generics have this very different type rule, arrays are synergistic, generics are immutable and can be erased. Therefore, the array provides runtime type safety, but no compile-type security. Generally speaking, there is no good mix between the 2, preferably using a list instead of an array.

4. preference is given to generics. generally it takes 2 steps for a class to be generic, declares a generic, and changes all objects to the generic parameter E. Finally, ensure that the conversion is safe and release the warning.

5. Give preference to generic methods. This makes it possible for new users to use the conversion type directly. More secure and convenient.

6. Leverage the flexibility of the Fraiti API with restricted wildcard. e represents the type parameter,? Represents a restricted wildcard character. Abide by the pecs principle, namely producer-extends,consumer-super. The parameter passed in is added to the class (producer), using extends. If it is a reduction (consumer), use super.

How will there be restricted wildcard characters? Applied to the type parameter E.

Eg:public static <t extendscomparable <? Super t> > T max (list<? Extends t> list)

All the comparable,comparator are consumers. So <t extends comparable <? Super t> > is better than <t extends comparable <T>>.

How will it be? Parameters are written directly to the object in the method.

Provides helper classes, which are passed into the generic method. While this generic method is processed in the Write object. This will eliminate the problem of compilation.

Add:

Generic Programming-wildcard characters

1) Generic boundary <textends A & B & c> syntax to declare that only one of them is a class, and only the first one behind extends is a class, and the others are only interfaces (and the extends meaning in the class/interface).

2) wildcard characters? :? similar to Object,list<?   Extends fruit> flist = new arraylist<apple> (); Instantiate fruit subclasses according to specific circumstances

3) Generic upper boundary: <textends a> must be a subclass of a

4) Generic lower boundary: <? Super T> must be the parent class of T


Effective Java experience, generics

Related Article

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.