JAVA generics and the use of wildcard characters

Source: Internet
Author: User
Tags getdate

The essence of generics is the parameterized type. That is, the data type being manipulated is specified as a parameter.

1.JDK 1.5/1.6 must explicitly write out the type of the generic type.

2.JDK 1.7/1.8 does not have to explicitly write out a generic type.

I. Generic declarations

Generic declarations can be made with <T>,<K,V>,<t extends number>, etc. where<t extends number> 's declaration method limits the range of T ,T can only be a subclass of number.

1. parameter types are used in the creation of classes, generic classes.

2. The parameter type is used in the creation of the interface, the generic interface.

3. parameter types are used in the creation of methods, generic methods. Note the declaration position of the generic <T>, after the modifier of the method, returns the value type before.

 Package me;  Public class Genetest<t> {    private  T date;      Public T getDate () {        return  date;    }      Public void setDate (T date) {        this. Date = date;    }} 
generic class
 Package me;  Public Interface Integene<e> {    public  E getData ();      Public void SetData (e e);}
generic interface
 Package me;  Public class megenetest {    publicstaticvoid  Gene (T test) {        System.out.println ( test);    }      public <T> t getDate (t test) {        return  test;    }}
Generic Methods

Two,? , a type wildcard character

several forms of wildcard characters:

1. unqualified wildcard characters,<?>.

2. Upper bound wildcard character , <? Extends Number>. Indicates that the parameter type can only be a subclass of number.

3. lower bound wildcard,<? Supper number>. Indicates that the parameter type can only be a parent of number.

 Packageme; Public classTest { Public Static voidMain (string[] args) {genetest<String> GTS =NewGenetest<>(); Gts.setdate ("This is a test!");                Print (GTS); Genetest<Integer> GTI =NewGenetest<>(); Gti.setdate (123);    Print (GTI); }         Public Static voidPrint (genetest<?>s)    {System.out.println (S.getdate ()); }}
use of wildcard characters

In the example code above, the parameters of the print () method use wildcards to pass in a generic class genetest<t> any type of argument. If you change the wildcard character to a specific type, such as a String type. The print () method can only pass in parameters of the generic class genetest<string> type.

A generic declaration indicates that, in the creation of a class, interface, method, you want to use an unknown data type, a data type that might be used in the future. It can be either an Integer type or a String type. Define its type as T ,K, or V , and so on.

at the time of instantiation, you must declare what type T is exactly.

When you define how a generic object is used, you do not know what type T is, it may be of type String, or it may be an Integer type. If you define T as a deterministic generic data type, the parameter can only be that data type. At this point, a wildcard is used instead of a generic data type that is determined.

use generics, wildcards to improve code reusability.

divide an object into declarations and use two parts. Generics focus on code reuse on type declarations, and wildcard characters focus on code reuse on the use. Generics are used to define uncertainties for internal data types, and wildcard characters are used to define the object type uncertainties used.

JAVA generics and the use of wildcard characters

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.