Java generic, java generic

Source: Internet
Author: User

Java generic, java generic

The generic name sounds very advanced, but it is not difficult to understand it.

When should I use generics? Why should I use generics.

Let me explain the reason.

public class Fanxing{    public static void main(String[] args)    {        Lei l1=new Lei("12354");        Lei l2=new Lei(5);    }}class Lei{    String str;    Integer in;    public Lei(String str)    {        this.str=str;    }    public Lei(Integer in)    {        this.in=in;    }}

From the code above, we can see that when we need to create two different types of objects

We need to reload different methods.

The generic model can solve this problem well.

class FanLei<T>{    T l;    public FanLei(T l)    {        this.l=l;    }}

Here T is similar to our value passing parameter, for example, the int char type parameter.

In this way, we do not need to reload new objects.

In general, that is to say, T is a certain type, it can be any type, And this type

You can decide, this avoids the need to reload as before.

Public class FanxianDemo <T> // as a parameter, consider the form parameter in the method similar to {private T [] array; // similar to the public void setT (T [] array) parameter) {this. array = array; // input the array type and obtain the type} public T [] getT () {return array;} public static void main (String [] args) {FanxianDemo <String> a = new FanxianDemo <String> (); String [] array = {"red", "White", "green"};. setT (array); // automatically obtain the type for (int n = 0; n <. getT (). length; n ++) {System. out. println (array [n]) ;}}
}

The preceding example shows that the type parameter is generic.

We passed in the String class above. We can also pass in the Integer class.

The following is an array of Integer objects.

This is the benefit of generics. You do not need to overload two constructors, one String and one Integer.

------------------------------------------------------------------

Another benefit is shown below. Iteration

ArrayList arr1 = new ArrayList (); arr1.add ("12354"); String obj = arr1.get (0); (error Reported)

Here, an error is reported because the Object obtained here is of the Object class.

We need to forcibly convert it to the String class.

In this case

ArrayList <String> arr=new ArrayList<String>();arr.add("12354");String obj1=arr.get(0);

In this case, we do not need to forcibly convert.

------------------------------------------------------------------

The two benefits of generics are described above.

First, reload is not required.

Second, no forced conversion type is required.

Generic applications are also used for methods and interfaces.

Most of them share the same role. (Initial understanding of cainiao)

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.