Java generic summary, java generic

Source: Internet
Author: User

Java generic summary, java generic

Generic summary:

1. generics have the ability to parameterize types. You can define classes or methods that use generic types. The compiler will replace generic types with specific types.

2. The main advantage of generics is the ability to detect errors during compilation rather than during runtime.

3. The generic class or method allows you to specify the object type that this class or method can contain. If you try to use a class or method with incompatible objects, the compiler may detect this error.

4. The generic type defined in the class, interface, or static method is called the form introspection type. Then, you can replace it with an actual specific type. The process of replacing the generic type is called the generic instantiation.

5. generic classes that do not use type parameters are called the original type, for example, ArrayList. The original type is used to be backward compatible with earlier Java versions.

6. There are three types of general type :? ,? Extends T ,? Super T. here T represents a generic type.

1) The first form ,? It is called an unrestricted wildcard? The extends Object is the same.

2) What is the second form? Extends T is called a restricted configuration, which represents a subtype of T or T.

3) What is the third form? Super T is called the lower limit wildcard, indicating a parent type of T or T.

7. Use a method called type elimination to implement generics. the compiler uses generic class information to compile code, but then eliminates it.

Therefore, generic information is unavailable at runtime. This method can make generic code backward compatible with legacy code of the original type.

8. You cannot use generic parameters to create objects.

9. You cannot use generic parameters to create arrays.

10. You cannot use tired generic parameters in a static environment.

11. You cannot use generic parameters in the exception class.

Related Article

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.