Understand Java generic wildcard characters? and the use

Source: Internet
Author: User

What is a generic type:

Generics are literally understood to mean that a class, interface, or method supports multiple types, making them broad, generalized, and more Generic. Java uses the object class to define the type can also implement generics, but the disadvantage is that the loss of the original type of information, in use easily caused Classcastexception.

Benefits of Java Generics:
    1. Makes the type in a class or method parameterized, and eventually achieves the effect of code Reuse. (do not use generics, you may need to define the class or method for each case Again)
    2. Implements the function of type checking, avoids Classcastexception. (this is relative to implementing generics using the object Type.) Because I can define each class once to implement the so-called type Checking. )
Generic Customizations: classes, interfaces, methods
//defines a generic class, with the same definition of an interface as a classclassG1<t>{T content;}//define generic methods, method header use <T> declaration, Note structureclassGMethod1 {//General generic method definitions     public Static<T>voidmethod1 (T Params) {}//The return value is also generic     public Static<E>e method2 (e params) {e content=params; returncontent; }}//The use of extends, which limits the scope of generics, equals or is a subclass of extendsclassG2<textendsNumber>{T content;}

Use of generics
    // Use of generic classes or interfaces    New arraylist<>();     // use of generic methods, using <> incoming types    in front of methods <string>gmethod1.method1 ("str");     <INTEGER>GMETHOD1.METHOD1 (new Integer (1));     // if T is used in the parameters of the argument, the <> in front of the method can be omitted, and the compiler can identify its type    by itself Gmethod1.method1 ("str");    GMETHOD1.METHOD1 (new Integer (1));

A wildcard character? When to use wildcard characters:

Wildcards are used only when modifying a variable , which makes it easy to refer to generics that contain multiple types;

     public Static voidmain () {//do not use a wildcard characterarraylist<object> arr =NewArraylist<object>(); //arraylist<object> arr = new arraylist<string> (); compilation does not pass, arr can only reference a collection containing Object//using wildcard charactersArraylist<?>arr2; ARR2=NewArraylist<string>(); ARR2=NewArraylist<integer>(); Arr2.get (0);//returns an object that is a wildcard that causes the original collection to contain type information that is lost and the use of WILDCARDS. //wildcards are typically used in method parameters so that the method can refer to a variety of generic collections. This is not the same as the paradigm method, here is just a reference variable    voidGmethod (arraylist<?extendsNumber>Param) {    }

As you can see, wildcards are easy to use while making the original collection contain type information missing.

Extends super keyword with wildcard characters

and defining the Extend concept in generics is completely different, and here's what it means.

        extends // number is the parent        of Integer, float New arraylist<integer>();         New arraylist<float>();         // ARR3 = new arraylist<string> (); compilation does not pass, String and number do not have an inheritance relationship         arr3.get (0);    // returned is a number object        Arr3.add (null// used wildcard-decorated set variable, only add (null), because the type contained in this collection has been determined, only the type information has been lost, add (Object) Or not .

Extensions: variables that do not use generics and another way
        // This allows for a variety of references, like wildcards, but is generally not recommended for this use         ArrayList a4;         New arraylist<string>();         New arraylist<integer>();        A4.add (new Integer (1));        A4.add (new String ("str")); // and wildcard references cannot add different, so the way can add multiple types of elements, but generally not recommended         a4.get (0);  // returned is an object .

Understand Java generic wildcard characters? and the use

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.