Effective generic parts in Java

Source: Internet
Author: User

Tag: Is the CER object type call error arraylist conversion extend compiler

Today will be effective Java (second edition) in the generic part of the reading, deep understanding of their own generic mastery of how unskilled, or need more practice.

Say less nonsense, on the point of focus:

1. Do not use prototypes

Like what:

New ArrayList ();

When you use the list reference to point to other lists with generics, there is no compilation error, only a rawtype warning, but ————

This is prone to trickery situations, such as when you point to a list<integer>, but add a string that does not have any compilation errors, but

When you take it out and turn it into an integer, the classcastexception appears, because you have a string ...

If you are unsure of the type, then use "?" This wildcard character can eliminate warnings and prevent all elements except null from being added.

2. Prioritize lists, not arrays

The reason is simple, the array is covariant, generics are not. For example, you have a list<string>[] array (assuming it is legal), if you use a object[] reference to point to the generic array,

This is certainly possible, because the array is covariant, but when you take this element out of the object[] and call the Get method to return an integer, the type conversion exception occurs. Therefore, generics

The array is illegal.

3. Preference for generics and generic methods

This needless to say, you can look at the collection of the source code, basically used generics, for the array storage this block, should be using reflection, using the Arrays.newinstance () method to generate a

An array of object references and then cast to an array of t[], which guarantees the correctness of the type at add or get (the compiler will help you get it done).

The generic method is a very powerful method:

 Public Static void int int j)

This generic method, which can automatically catch exceptions, including when you pass in a parameter of list<?>, is just what is captured at this time. This wildcard is recommended to be used only in void methods.

4.PECS principle

That is, the producer of T (producer) is a generic type of < Extends T>,t consumer (consumer) generic for <? Super t>.

The reason is simple, the producer mainly provides the method is the Get () method, and Java at run time the generic is erased, that is, here the t becomes the object,

At the "boundary" of a method called or left, the compiler transforms according to the generic parameter T, which means that the object type obtained by invoking the producer Get method is uniformly

to the T type, indicating that no matter what type it is, it is obtained as long as the T type can be connected (the subtype of T). The main method provided by the consumer is add ()

(or whatever consume (), etc.), this indicates that the type of consumer consumption is as long as the parent type of T, which is the object that is consumed when the Add () method is called

is the parent type of T or T, guaranteeing a lower bound.

Summary: To better understand the generics, must be more hands-on to practice, to think, grasp the generics at run time is erased this feature, you can master the use of most generics

Effective generic parts in Java

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.