Generics use in Java

Source: Internet
Author: User

Generics: After 1.5 The increase security 1 generics determine the type of the collection container. 2 <> receive a data type (reference data type) ArrayList lis = new ArrayList () Objective: To convert run-time errors to compile time and improve security! 3 You do not need to force type conversions. More secure!

Erase generics: Generics are used at compile time! Erase directly with use. There is no generics at the end of compilation.

Benefits: Generics are used, and type conversions are not required for coercion? (Multiple data types) why? Because there is only one data type in the container. After the data is removed, the data is processed in detail! A String is important. In the development of reality.

Generic Custom classes:

1 2 3 4 5 6 7 8 9 10 11 12 13-14 Use generic class Tools<t> {private T obj;     public void SetObject (T obj) {this.obj = obj;     Public T GetObject () {return this.obj; }}</t>

Generics Apply to methods:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The class Test<e> {    //generic Used on methods     //when a method in a class is used only if the type on the class is determined to be determined, use the method            //generics     //otherwise without generic     public void func (e e)      {        system.out.println (e);     }       //the method used by the generic:    method alone is indeterminate, and any type is OK     //this   When the    method is invoked, the type is determined     public <t> void Show (T e)     {         system.out.println (e); &NBSP;&NBSP;&NBSP;&NBSP}               // Static is loaded as the class is loaded, this is not the type, so the static methodYou can only define generics     public static <w> void function (W e)//test.function ()        {        system.out.println (e);     }         public void ff (int a,int b)     {        system.out.println (A+B); &NBSP;&NBSP;&NBSP;&NBSP}}</w></t></e>

Generics are defined on an interface:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The /generics for interface   interface Inter<t> {     public void Show (t); Class Test implements inter<string> {    public void Show (String t)      {              } class demo<e>   Implements inter<e> {    public void show (E t)     {         system.out.println (t); &NBSP;&NBSP;&NBSP;&NBSP}} class  Demo9 {    public static void Main (string[) args)  & nbsp;  {        Demo<string> d = new demo<string> (); ()//Error &NBSP;&NBSP;&NBSP;&NBSP} </string></string></e></e></string></t>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.