Java: Generics

Source: Internet
Author: User

A simple introduction to generics

1. Introduction of Generics

The new feature of generics introduced in JDK 1.5 is that the essence of generics is the application of the parameterized type (parameterized Types), which means that the data type of the operation is specified as a parameter, and then the data must be used to conform to the specified type. This parameterized type can be used in classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.

2. Why the introduction of generics

Generic thinking begins in the C + + template (Templates), and when Java has no generics, type generalization can only be achieved by using the object type as a combination of all types of parent types and type casts of two features. For example, in access to a hash table, JDK 1.5 uses the HashMap get () method before the return value is an object. Since all of the types in Java inherit from Java.lang.Object, that object can be converted to any type of object. This leads to a flaw: During compilation, the compiler cannot check whether the cast of this object succeeds, only the programmer and the virtual machine during compilation know what type of object this is. This results in an increased risk of classcastexception during operation.

3. "Pseudo-generics" in Java

Before explaining why Java is a pseudo-generic, let's briefly introduce the "True generics" of C #:

C # generics are new in C # 2.0, and C #, regardless of language level or CLR, provides support for generics, so when a C # generic class or method is compiled into Microsoft intermediate Language (MSIL), it still contains metadata that is represented as a type parameter, meaning that generics are real. Types do not become native types, but are implemented by type bloat, generating their own virtual methods and type data at run time. So C # is "real generics."

Generics in Java exist only in the source program, in the compiled bytecode, the generics are replaced with the original type (raw type), and the cast code is inserted where appropriate. As a result,,arraylist<int> and arraylist<string> are the same classes for runtime java. So generics are a syntactic sugar in Java, and the generic implementation method in Java is called type Erasure, and generics based on this approach are called pseudo generics.

4. Basic terminology in Generics

Take arraylist<e> and arraylist<integer> as an example:

(1)"arraylist<e>" is called the generic type ;

(2) "E" in arraylist<e> is called a type variable or a type parameter ;

(3)"arraylist<integer>" is called parameterized type ;

(4) The "Integer" in Arraylist<integer> is called an instance of the type parameter , or the actual type parameter ;

(5) " <Integer>" in arraylist<integer> reads "type of Integer";

(6)"ArrayList" is called the original type

Second, the use of generics

1. Definition and use of generic classes

The generic class (generic Class) is a class that has one or more type variables. To define a generic class, simply add "<>" after the class name, and then add the type parameter to the angle brackets. Here is a simple comparison of non-generic classes and generic class code implementations:

Non-generic box class public class Box {    Private object object;    public void Set (Object object) {This.object = object;}    public object Get () {return object;}}

Contrast

/** * Generic Box class * @param <T> is the type of the box class intrinsic value */public class Box<t> {    //T stands for "type"    private t T;    public void set (T t) {this.t = t;}    Public T get () {return T;}}

  

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