Use of wildcard characters in Java generics

Source: Internet
Author: User

Learning GoalsMastering the use of the wildcard character "?"Mastering settings for restricted genericsMastering the limitations of generics and subclass inheritance
match any type of wildcard characterreference passing of objects in development is most common, but if the generic type must match in the operation of the generic class to be passed. Otherwise, it cannot be delivered.
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo12{public static void Main (String args[]) {info<string> i = new info<string> ();//Use S Tring is a generic type I.setvar ("LX");//set Content fun (i);} public static void Fun (Info<object> temp) {//receives an object generic type Info object System.out.println ("content:" + temp);};

there was an error at compile time:
when generic objects are passed by reference, the types must be identical. If you do not want to pass now, you can cancel the generics in the fun method. As shown below:
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo13{public static void Main (String args[]) {info<string> i = new info<string> ();//Use S Tring is a generic type I.setvar ("wwww");//set Content fun (i);} public static void Fun (Info temp) {//receives an object generic type Info object System.out.println ("content:" + temp);};
A warning alert was found:
It does not affect the operation of the program:
The above does have improved functionality, but the code seems to be somewhat inappropriate, after all the generics have been specified. If you use? It? As shown below:
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo14{public static void Main (String args[]) {info<string> i = new info<string> ();//Use S Tring is a generic type I.setvar ("mldn");//set Content fun (i);} public static void Fun (Info<?> temp) {//can receive arbitrary generic object System.out.println ("content:" + temp);}};
Can run as usual without warning.Note: If you use the? means that you can receive arbitrary content, but this content cannot be modified directly using the <?> modified generic object. As shown below:
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo15{public static void Main (String args[]) {info<?> i = new info<string> ();//Using String For generic type I.setvar ("mldn");//set content}};

errors were found, warnings and run results are as follows:


in other words, the user <?> can only receive. But cannot be modified.
Restricted genericsbefore you set a generic type, you can actually set it arbitrarily, as long as the class is set. In Java generics, however, you can specify the upper and lower bounds of a generic type. The upper bound of the range is declared with the extends keyword, which indicates that the parameterized type is either the specified type or a subclass of this type. The range lower limit is declared with super, which indicates that the parameterized type may be the type specified, or the parent type of this type, until the object class. set the upper limit:declared object: class name <? extends class > object namedefinition class: [access rights] class name < generic label extends class >{}set the lower limit:declared object: class name <? Super Class > Object namedefinition class: [access rights] class name < generic label Super class >{}
Set Upper Limit
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}};  public class Genericsdemo17{public static void Main (String args[]) {info<integer> i1 = new info<integer> ();// A generic object that declares an integer info<float> i2 = new info<float> ();//A generic object that declares Float I1.setvar (30);//Set integer, Auto-boxing I2.setvar ( 30.1F);//Set decimal, Auto boxing fun (i1); fun (I2);} public static void Fun (INFO<? extends Number> temp) {//can only receive subclasses of number and its number System.out.print (temp + ",");}};


If you pass a method that is not a generic label other than number and its subclasses, the error will be as follows:
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo18{public static void Main (String args[]) {info<string> i1 = new info<string> ();//Declaration The generic object of string I1.setvar ("Hello"); Fun (I1);} public static void Fun (INFO<? extends Number> temp) {//can only receive subclasses of number and its number System.out.print (temp + ",");}};

generics can also be used in a class, as follows:
Class Info<t extends number>{//here generics can only be numeric type private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}};  public class Genericsdemo19{public static void Main (String args[]) {info<integer> i1 = new info<integer> ();// A generic object that declares an integer}};
If you now set the string type when using info, the error will occur at compile time.
Class Info<t extends number>{//here generics can only be numeric type private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo20{public static void Main (String args[]) {info<string> i1 = new info<string> ();//Declaration integer-Generic Object}};
Set lower LimitWhen you use generics that can only be applied to this class and to the parent class type, you must use the generic range lower bound configuration
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo21{public static void Main (String args[]) {info<string> i1 = new info<string> ();//Declaration The generic object of string info<object> i2 = new info<object> (),//The generic object that declares object I1.setvar ("Hello"), I2.setvar (New object ( ); Fun (I1), Fun (I2);} public static void Fun (INFO< Super string> temp) {//can only receive generic System.out.print of type String or object (temp + ",");}};



If integer is now used as the generic type, the lower bound of the generic is not satisfied. As shown below:
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}};  public class Genericsdemo22{public static void Main (String args[]) {info<integer> i1 = new info<integer> ();// A generic object that declares an integer i1.setvar (a); Fun (I1);} public static void Fun (INFO< Super string> temp) {//can only receive generic System.out.print of type String or object (temp + ",");}};

Explanation: Limitations of generics and subclass inheritancea subclass of a class can be instantiated for its parent class through object polymorphism, but in a generic operation, the generic type of the subclass cannot be received using the generic type of the parent class, for example:info<string> cannot be received using info<object>.

examples are as follows:
Class Info<t>{private T var;//define generic variable public void SetVar (T var) {this.var = var;} Public T GetVar () {return this.var;} Public String toString () {//Direct print return this.var.toString ();}}; public class Genericsdemo23{public static void Main (String args[]) {info<string> i1 = new info<string> ();//Generics The type is stringinfo<object> i2 = null; i2 = I1;}};

An error message was found.
Summary:1, use? You can receive arbitrary generic types. 2, the maximum of the generic type:? extends type. 3, the lower limit of the generic type is not too much. 4. Understand why inheritance between generic subclasses cannot be converted directly.


Use of wildcard characters in Java generics

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.