Generic Basics of Interview summary

Source: Internet
Author: User
first, the concept of generics

A parameterized type that is parameterized by the original specific type of the type.
A generic type, logically, can be viewed as a number of different types, actually the same type. such as list and list are actually list type two, type wildcard characters

Type wildcard characters are generally used? Instead of a specific type argument. (note is the argument.) is not a formal parameter)
List<?> can logically refer to all list< specific types of arguments, such as:list<string>,list<integer>, etc.
Type wildcard upper bound: list<? Extends number>//passing parameters can only be number or subclass
Lower limit of type wildcard: list<? The parameters passed by Super number>//can only be number or parent class Three, the erase mechanism of the generic type

Generics work in the code compilation phase, and in the compilation process, the generic information is erased when the generic result is correctly validated. For example, the class file after the successful compilation does not contain any generic information. Generic information does not enter the Run-time phase.
(Supplemental: The JVM does not know generic information at run time because of the erasure mechanism of the JVM generics.) For example, you can give array[1] a ArrayList without an exception, but do a type conversion when you take out the data, so there is classcastexception, if you can declare a generic array, In this case, there will be no warnings or errors at compile time, and errors will occur only at run time. Four, the benefits of generics 1, type safety.

By knowing the type limitations of variables defined with generics, the compiler can improve the type safety of Java programs more effectively. 2, eliminate the mandatory type conversion.

Eliminates many coercion type conversions in the source code. This makes the code more readable and reduces the chance of error. All casts are automatic and implicit, increasing the reuse rate of the code. v. Considerations for generic Use

(1) A generic type parameter can only be a class type (including a custom class) and cannot be a simple type.
(2) The type parameter of the generic type can have multiple.
(3) The instanceof operation cannot be used on the exact generic type.
(4) Cannot create an array of exact generic types.
Add: After using generics, the types of objects that can be stored in the container are already determined, and if you put other types of objects into the container, the compiler will also give you an error, and the compiler can help you to transform the object, which reduces the rate of error in programming, but does not improve performance at runtime. At most, the compilation speed is affected, because the generic erasure mechanism does not affect the running time.

Reference article: http://www.cnblogs.com/lwbqqyumidi/p/3837629.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.