Java capture and conversion

Source: Internet
Author: User

In general, the usage is no different from the usage, but there is one situation that requires special use <?> Instead of the native type (because unspecified wildcard types are captured and converted to the exact type ).

For example:

Holder <T> Holder (T val) {value = set (T val) {value = T get () {<T> f1 (Holder <T> = f2 (Holder <?> = Holder <Integer> (1 = Object (); f2 (rawBasic); Holder <?> Wildcarded = Holder <Double> (1.0

Capture conversions allow us to bypass the limits of the compiler when processing wildcards. When f2 () calls f1 (), it knows that this is safe because its own holder parameter must be Holder for some unknown V <V>. At the same time, the type parameter T is introduced into the method signature and is not bound to any other type parameter. It can also represent any unknown type. Therefore, the Holder of some unknown T may also be the Box of some unknown V.

The type parameters in f1 () are exact and there are no wildcards or boundaries. In f2 (), the Holder parameter is an unbounded wildcard, so it looks unknown. However, in f2 (), f1 () is called, and the parameter type is captured when f2 () is called. Therefore, it can ().

: T cannot be returned from f2 () Because T is unknown for f2.

Generally, APIs with Wildcards are simpler than APIs with generic methods. Increasing type names in more complex method declarations reduces the readability of declarations. Because the name can always be restored through proprietary capture and conversion as needed, this method can keep the API clean without deleting useful information.

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.