J2se jdk5.0 Learning 1 (generic)

Source: Internet
Author: User

Benefits of generics

Java
Introducing generics in a language is a major enhancement in functionality. Not only have the language, type system, and compiler greatly changed to support generics, but also the class libraries have been greatly revised. Therefore, many important classes, such as the collection framework, all of them have become generic. This brings many benefits:

·Type security.The main objective of generics is to improve the type security of Java programs. By knowing the type limits of variables defined using generics, the compiler can validate the type assumptions to a much higher degree. Without generics, these assumptions exist only in the programmer's mind (or, if Lucky, in code comments ).

Java
A popular technique in a program is to define such a set, that is, its elements or keys are of the common type, such as "string list" or "String
String ing to string ". By capturing this additional type information in the variable declaration, generics allow the compiler to implement these additional type constraints. Type errors can now be captured during compilation, rather than displayed as classcastexception at runtime. Moving type checks from runtime to compilation helps you locate errors more easily and improve program reliability.

·Eliminate forced type conversion.One additional benefit of generics is that many forced type conversions in source code are eliminated. This makes the code more readable and reduces the chance of errors.

 

·Potential performance benefits.Generics are possible for greater optimization. In the initial implementation of generics, the compiler inserts forced type conversion (without generics, the programmer will specify these forced type conversion) into the generated bytecode. However, the fact that more types of information can be used in compilers makes it possible to optimize future JVM versions.

Because of the implementation method of generics, it supports generics (almost) without JVM or class file changes. All work is done in the compiler. The Compiler generates code similar to the code written when there is no generic type (and forced type conversion), but it ensures type security better.

 

 

 

Type parameter

When defining a generic class or declaring a variable of a generic class, use angle brackets to specify a formal type parameter. The relationship between a formal type parameter and an actual type parameter is similar to that between a formal method parameter and an actual method parameter. Only a type parameter represents a type, not a value. The type parameters in a generic class can be used almost anywhere where the class name can be used. Except for the exception type, enumeration, or anonymous internal class, any class can have type parameters.

 

 

 

Name type parameters

The recommended naming convention is to use a single uppercase letter name as the type parameter. It also reflects the assumption that most generic classes will have a small number of type parameters. For common generic modes, the recommended names are:

· K --
Key, such as the ing key.

· V --
Value, such as the content of list and set, or the value in map.

· E --
Exception class.

· T --
Generic.

 

 

 

Erased

· Java
The generics in the language are basically fully implemented in the compiler. The Compiler performs type check and type inference, and then generates common non-generic bytecode. This implementation technique is called Erasure (the compiler uses generic type information to ensure type security and then clears bytecode before it is generated ).

· Generics are basically implemented in the Java compiler rather than in the Runtime Library. Therefore, when generating bytecode, almost all information about generic types is erased. In other words, the code generated by the compiler is basically the same as the code that you manually write without generics and checks program type security for forced type conversion.

· When a wildcard class bytecode is generated, the compiler replaces the type parameter with the erasure of the type parameter. For an unrestricted type parameter (<v>), The erased object is used. For the upper limit type parameter (<k extends comparable <k>), its erasure is the erasure of its upper limit (in this example, it is comparable. For type parameters with multiple restrictions, use the leftmost limit to erase them.

 

 

 

Generic Method

Why do you choose to use the generic method instead of adding type T to the class definition? (At least) There are two possible cases:

· When the generic method is static, class type parameters cannot be used in this case.

· When the type constraints on T are partial to the method, this means that the same method signature is not used in another method of the same class.
Type T constraints. By making the type parameter of the generic method partial to the method, you can simplify the signature of the closed type.

 

 

 

Backward compatibility

An important goal of introducing generics in Java is to maintain backward compatibility. Although many classes in the standard class library of JDK 5.0, such as the collection framework, have been generic, but use collection classes (such as hashmap and arraylist) the existing code will continue to work in JDK 5.0 without modification. Of course, existing code that does not use generics won't win the security benefits of generics.

Related reading materials:

Java Theory and Practice: Understanding generics

Http://www-128.ibm.com/developerworks/cn/java/j-jtp01255.html

Introduction to generics in jdk5.0

Http://www6.software.ibm.com/developerworks/cn/education/java/j-generics/tutorial/index.html

Diagnose Java code: Easy To Understand Java generics

Http://www.ibm.com/developerworks/cn/java/j-djc02113/index.html

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.