Java Technology _java thousand questions (0040) _ What is generics

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is a generic type

Generics are a new feature of Java SE 1.5, where generics are parameterized types, meaning that the data type being manipulated is specified as a parameter.
This type of parameter can be used in the creation of classes, interfaces, and methods, called generic classes, generic interfaces, and generic methods, respectively.
How to use generic methods, generic classes/interfaces See here: how generics work
In the absence of generics, the "arbitrariness" of the arguments by reference to the type object, the disadvantage of "arbitrariness" is the explicit coercion of type conversions, which require the developer to be able to predict the actual parameter type.
In the case of coercion of type conversion errors, the compiler may not prompt for an error and an exception occurs at run time, which is a security risk.
Therefore, the benefits of introducing generics into the Java language are safe and simple, checking for type safety at compile time, and all casts are automatic and implicit to improve the rate of reuse of code.

2, generics have those characteristics
  1. Generic type parameters can only be class types (including custom classes) and cannot be underlying types (such as int,double and Char).
  2. The same generic can correspond to multiple versions (because the parameter types are indeterminate), and different versions of the generic class instances are incompatible.
  3. The generic type of a class can have more than one.
  4. There is no inheritance relationship between generics, even if string inherits object. The following code is illegal
    List ls = new ArrayList ();
    List lo = ls;
  5. A generic type parameter can use the extends statement, for example. It is customary to call the bounded type.
  6. The type parameter of a generic can also be a wildcard type. such as Class

Java Technology _java thousand questions (0040) _ What is 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.