What is Java generics (being organized)

Source: Internet
Author: User

Why use generics?

Introduction Example

public class Generictest {public    static void Main (string[] args) {        list list = new ArrayList ();        List.add ("Qqyumidi");        List.add ("corn");        List.add (+);        for (int i = 0; i < list.size (); i++) {            String name = (string) list.get (i);//1            System.out.println ("Name:" + N AME);}}}    

  

The code above does not have a problem with the compiler, but it will be an error during operation. Because the list's Add method parameter is object, if you do not know the list type, through a strong value, this will be an error. 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 an object is placed in a collection, the collection does not remember the type of the object, and when the object is removed from the collection again, the compiled type of the object is changed to type object, but the type of the runtime is still its own type.

2. As a result,//1 the collection elements to the artificial conversion to the specific target type, and very prone to java.lang.ClassCastException anomalies

(This example, I found in a blog, I can read the introduction of the generic example, but I executed the error, the wrong content is java:4: Error:cannot Find Symbol, may be my JDK version is wrong, I want to put the example of generics as soon as possible to finish, This mistake will be put here for the time being. Know the specific wrong friend, please leave me a message in the comment area, thank you. )

How do you get the collection to remember the various types of elements within the collection, and to be able to achieve the java.lang.ClassCastException exception at run time without problems at compile time? The answer is to use generics.

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. Then the type is parameterized by the original specific type, similar to the variable parameter in the method, the type is also defined as the Parameter form (called the type parameter), and then the specific type (type argument) is passed in when it is used

public class Generictest {public    static void Main (string[] args) {        list<string> List = new Arraylist<str Ing> ();        List.add ("Qqyumidi");        List.add ("corn");        List.add (+);   1  hint compilation error for        (int i = 0; i < list.size (); i++) {            String name = List.get (i);//2            System.out.println ( "Name:" + name);}}}    

  

What is Java generics (being organized)

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.