What is generics in Java

Source: Internet
Author: User

I. The concept of generics (why generics are needed)?

First, let's look at the following short code:

1 public class Generictest {2  3 public     static void Main (string[] args) {4         list list = new ArrayList (); 5
   
    list.add ("Qqyumidi"); 6         list.add ("corn"); 7         List.add (+); 8  9 for         (int i = 0; i < list.size (); i++) {ten             String name = ( String) List.get (i); 111             System.out.println ("Name:" + name);         }13     }14}
   

Defines a collection of list types, with a value of two string types added to it, followed by an integer type. This is entirely permissible because the default type of the list at this time is type object. In the subsequent loop, it is easy to get an error similar to//1 in that it has been added to the list with an integer value or other encoding reason. Because the compile phase is normal, a "java.lang.ClassCastException" exception occurs at run time. Therefore, this kind of error encoding process is not easy to find.

In the coding process as above, we found that there are two main problems:

1. When we put an object into the collection, the collection does not remember the type of the object, and when the object is removed from the collection again, the compilation type of the object is changed to the object type, but its run-time type is still its own type.

2. As a result, it is necessary to//1 the collection element into the specific target type, and the "java.lang.ClassCastException" exception can easily occur.

So is there any way that the collection can remember the types of elements within the collection and be able to achieve the "java.lang.ClassCastException" exception when the runtime does not have a problem at compile time? The answer is to use generics.

Two. What is a generic type?

Generic, which is the parameterized type. When referring to a parameter, it is most familiar to define the method when the physical parameter is called and then pass the argument when calling this method. So how do parameterized types understand? As the name implies, the type is parameterized by the original specific type, similar to the variable parameter in the method, when the type is also defined as a Parameter form (which can be called a type parameter), and then the specific type (type argument) is passed in when using/calling.

Looks a bit complicated, first we look at the above example using generic notation.

1 public class Generictest {2  3 public     static void Main (string[] args) {4/         * 5         List list = new ArrayList (); 6         List.add ("Qqyumidi"), 7         list.add ("corn"), 8         List.add (+), 9         */10 one         list<string> list = New Arraylist<string> (),         list.add ("Qqyumidi"),         list.add ("corn"),         //list.add (+)   ; 1  hint compilation error for         (int i = 0; i < list.size (); i++) {n             String name = List.get (i);//218             System . Out.println ("Name:" + name);         }20     }21}

After using the generic notation, a compilation error occurs when you want to add an object of type integer at//1, by List<string>, directly qualifying only elements of type String in the list collection, so there is no need to force type conversions at//2, because at this point , the collection remembers the type information of the element, and the compiler has been able to confirm that it is of type string.

What is generics in Java

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.