Wildcard characters in Java generics

Source: Internet
Author: User

A wildcard character in a Java generic can directly define the parameters of a generic type. Instead of defining the function as a generic function.

 Public classGenericstest { Public Static voidMain (string[] args) {List<String> name =NewArraylist<string>(); List<Integer> age =NewArraylist<integer>(); List<Number> number =NewArraylist<number>(); Name.add ("Icon"); Age.add (18); Number.add (314); //Getupernumber (name);//1Getupernumber (age);//2Getupernumber (number);//3          }
Wildcard method implementation, very concise Public Static voidGetData (list<?>data) {System.out.println ("Data:" + data.get (0)); } Public Static voidGetupernumber (list<, extends number>data) {System.out.println ("Data:" + data.get (0)); }

Common Way implementation
   <T>void GetData (list<t>  data) {      System.out.println ("Data:" + data.get (0)) ;   }      <t extends number>void Getupernumber (list<t>  data) {          System.out.println ("Data:" + data.get (0));   }

}

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.