Java Experience---Generics

Source: Internet
Author: User
Tags class definition

1. Generic type of collection class

generics are a security mechanism that occurs after JDK1.5. < > To specify the type of elements in a container

Benefits: 1. Transfer problems that occur during the runtime to the compile time.

2. Avoid the hassle of forcing type conversions

2. The idea of a generic type

eliminates coercion type conversions in code when fetching a collection element, such as specifying a specific element type that is allowed to be added in a collection, and then adding an element's type check to the compilation Link implementation collection to prevent someone from saving elements of an unintended type to the collection.

¯ Advantages
Type errors can be exposed at compile time rather than at runtime (throw classcastexception), which helps to troubleshoot early and improve the reliability of the program.

3. Note:

(1) After the JDK5.0 set class definition as vector has been a generic transformation, there are generic classes with multiple type parameters, such as map<k, v> have two type parameters, representing key-value mappings.

(2) generics in the Java language maintain backward compatibility, that is, we can continue to follow past practices without generics.

(3) When compiling a collection class that does not have a generic mechanism enabled in a high version development environment, the compilation hint information is output.

4. The generic class is used when the reference data type of the operation in the class cannot be determined.

You can define a type parameter if you are unsure of what type of data to manipulate in the class.

user, what type of data is passed, then what type of information is being manipulated in the class.

5. To better limit the type parameters when defining generic classes, the generic mechanism also allows additional constraints on the type parameters.

¯ <t extends person>

the type parameter is a subclass of the person class or the person class

6. Generic method: The definition of generics on a method.

The ¯ generic is defined on a class, which acts on the entire class.

¯ define generics on a method so that you can manipulate different types of data using a generic method.

¯ Format:<t> return type method name (t)

7. Static methods may not use generics defined in the class.

because generics in a class need to specify a specific type when the object is initialized.

and static precedence exists with objects.

A generic is defined on a static method, and must be written after the static keyword. Returns the front of a value type.

8. The generic method allows the method to be independent of the class and produce changes. Here is a basic guideline:

   Whenever possible, you should use a generic method whenever you can; that is, if you use a generic method to replace the generics of this class, then you should use only the generic method, because it makes things clearer.

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.