Java Fundamentals: Generics

Source: Internet
Author: User

The same is the problem encountered during the interview, in the normal process of writing code, often used to generic programming, such as the use of various sets of methods, such as ArrayList, HashMap, List, etc., are used to generics. But when the interviewer let himself introduce the system of generic programming, he also suddenly confused, his understanding of the so-called generics, is the declaration of the container variable, the type as a parameter declaration, for the other features and benefits of generics, they do not have a very systematic understanding, hereby summarized in this system, Java is always a very important part of the HA, is the generic programming.

Before Java added generic classes, the design of generics was implemented by inheritance, i.e., ArrayList, which only maintained an array of object references, and when it was stored in other types, it was converted by a strong value type, which would cause problems and look at the following code first:

  Public classTest { Public Static voidMain (string[] args) {List List=NewArrayList (); List.add ("Qqyumidi"); List.add ("Corn"); List.add (100);  for(inti = 0; I < list.size (); i++) {String name= (String) list.get (i);//1System.out.println ("Name:" +name); }     } }

In the code, the list variable is maintained with the data of an object, and the object type is the parent class of all types, all of which can be deposited into the list of any type of instance, in which two string types and an instance of an integer type are stored. This will result in reading the variables in the array, you can not predict in advance what type of variables stored in the data, resulting in a forced type conversion exception, such as in the example, will appear to read to the third integer variable when the error. This is the problem with Java before the generic class, and it is only in the programmer's own mind that it is predetermined which class is the container of what type of variable to store, not in the code, which makes the code safe and readable.

To solve this problem, Java presents the concept of generic classes, generics, or "parameterized types". that is, when declaring a method, you need to declare the type of the parameter, when declaring the generic variable, you also need to declare the variables in the type, the original type programming parameters, in the declaration of the process of declaration in. You can look at the following example:

 Public classTest { Public Static voidMain (string[] args) {/*List List = new ArrayList ();        List.add ("Qqyumidi");        List.add ("corn");        List.add (100); */List<String> list =NewArraylist<string>(); List.add ("Qqyumidi"); List.add ("Corn"); //List.add (100); //1 hint compilation error         for(inti = 0; I < list.size (); i++) {String name= List.get (i);//2System.out.println ("Name:" +name); }    }}

In this example, when the list is declared, the string is declared as a parameter into the list variable, then the list is a list that is declared to have only a string type, and when you want to insert an integer into the list, the compiler will check the error. This undoubtedly increases the readability and security of the code. This is the benefit of generics from the perspective of a generic class.

So, from the perspective of the consumer of a generic class, the generic benefit is a container class that, after using generics, can increase the reusability of the code without having to write a specific container class for each variable, such as a string type, to write a container that can only store string types, and the integer type , and can only write a container class that can only store integer type, in the process of using generics, we only need to use the form of <>, the type of the parameter to be stored can only need to write a container class code, and this container class can be used to reprint various types of instances, This is the benefit of generics on the other hand. The following simple code for the previous custom generic programming:

class Box<t> {    private  T data;      Public box () {    }    public  Box (T data) {        setData (data);    }      Public T GetData () {        return  data;    }      Public void setData (T data) {        this. data = data;    }} 

Java Fundamentals: 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.