Thoughts on the generic jdk1.5

Source: Internet
Author: User
Tags java reference

Let's take a look at the following small program.

Import java. util. arraylist;
Import java. util. List;

// Declare a generic class
Public class genericclass ...{

Public static void main (string [] ARGs )...{
List <integer> List = new arraylist <integer> ();
List. Add (1 );
List. Add (2 );
New product (). PRT (list );
}
}

// Create a class to illustrate the problem
Class product ...{
Public void PRT (list <string> lt )...{
System. Out. println (LT );
}
}

 

The compiler reports an error, indicating that PRT (Java. util. List <java. Lang. String>) in the product cannot be applied to (Java. util. List <java. Lang. Integer> ). Previously, list <integer> List = new arraylist <integer> () is defined, while public void PRT (list <string> lt) is called to use list as a parameter, it must be an incorrect usage. The compiler reports errors correctly and helps you avoid potential errors. Note the functions of generics: Although list <integer> and list <string> are both list, the compiler regards them as different classes and cannot replace them with each other. This is the basic principle of generics. You can think of them as class1 and class2. Of course, you cannot pass a class2 parameter method to a method that requires class1 parameters.

Let's take a look at the situation where the above program is slightly changed.

Import java. util. arraylist;
Import java. util. List;

// Declare a generic class
Public class genericclass ...{

Public static void main (string [] ARGs )...{
List <integer> List = new arraylist <integer> ();
List. Add (1 );
List. Add (2 );
New product (). PRT (list );
}
}

// Create a class to illustrate the problem
Class product <t> ...{
Public void PRT (list <string> lt )...{
System. Out. println (LT );
}
}

The compiler says it uses unchecked or unsafe operations, but can be compiled and run to display [1, 2].

Why? Is the red one <t> changing so much? It doesn't seem like pulling the results just now. How can this time be done?

The reason is: in the first program, the class product is changed to the class product <t>, although the type T is never used in the product code, however, this definition changes a common class to a raw class ). Generic class product <t> does not exist during JVM runtime. The original type of product is not of type security. Because in the new product. in PRT (list), the original type of product is used, so the list in it will be wiped to the original type, so the type is consistent, it can be run without compilation errors.

Although the program can run at this time, it is undoubtedly wrong to use the generic method. Here I just want to analyze some details about generics. You need to understand the details of generics through this detail (trial, original type ). I will not talk about the specific content. Many books have it. Recommended Java reference Daquan 5 edition page264 and core Java I 7 edition Chapter 13. Generic programming

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.