Dark Horse programmer _java Generics

Source: Internet
Author: User

-----Java Training, Android training, iOS training,. NET training, look forward to communicating with you!

because I have limited knowledge, can not be described in detail generics, so this article is only a rough description of the relevant knowledge of generics, in the future learning process will be modified this article, I hope this article will not bring confusion to the reader, hereby declares

What is a generic type?

Generics (Generic type or generics) are an extension of the Java language type system to support the creation of classes that can be parameterized by type. You can think of a type parameter as a placeholder for the type that you specify when you use a parameterized type, as if the form parameter of the method is a placeholder for the value passed at run time.

generics are provided for use by the Javac compiler to qualify input types in the collection, allowing the compiler to block illegal input in the source program.

Why use generics

Before using generics, the elements that will be stored in the collection can be any data type, and when an element is fetched from the collection, the type of the element is type object, and the element needs to be cast down to a specific type. Like what:

List myintlist = new LinkedList ();//1

Myintlist.add (New Integer (0));//2

integer x = (integer) myintlist.iterator (). Next ();//3

This forced type conversion of the third row may cause a run-time error.

The idea of generics is that the programmer specifies the type so that the collection can only accommodate elements of that type .

using generics:

list<integer> myintlist = new linkedlist<integer> ();//1 '

Myintlist.add (New Integer (0));//2 '

Integer x = Myintlist.iterator (). Next ();//3 '

Casting the coercion type of the third row into the list type description of the first row, the compiler checks the correctness of the type for us. In this way, the readability and robustness of the code are enhanced.

Benefits of Generics

1 type safety : The primary goal of generics is to improve the type safety of Java programs, the variable types defined by generics are limited, and the compiler can make assumptions about the type of validation on a much higher Chengdu. Without generics, these assumptions can only exist in the programmer's mind.

2 elimination of forced type conversions: One incidental benefit of generics is to eliminate many of the forced types of conversions in the source code, which makes the code more readable and reduces the probability of errors

In short: The advent of generics, the first can be run-time errors ahead of the compilation period, the second reduces the type conversion, simplifying the code to improve the reading.

Generic usage principles

1. A generic type parameter can only be a class type (including a custom class) and cannot be a simple type.

2, the same generic can correspond to multiple versions (because the parameter type is indeterminate), different versions of the generic class instances are incompatible.

3, generic type parameters can have more than one.

4. Parameter types of generics can use extends statements, such as <t extends Superclass>. Become a "bounded type" in practice.

5. The parameter type of a generic type can also be a wildcard type. For example class<?> ClassType = Class.forName (Java.lang.String);

Dark Horse programmer _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.