Java JDK8 Learning notes--18th. Customizing generics, enumerations, and annotations

Source: Internet
Author: User

18th. Customizing generics, enumerations, and annotations 18.1 custom generics

Generic definition:

(1) Only generic syntax defined on a method
(2) extends and Super keywords to limit the available types of generics
(3)? Use of type wildcard characters

18.1.1 use extends with?

1, if the extends after specifying the class and interface, want to specify another interface, you can use the & connection.

2. If B is a subclass of a, and node< b> can be regarded as a node< a>, then node is said to be either co-denatured or resilient. Java generics do not have a covariance, can you use type wildcard characters? and extends to declare the variable so that it achieves a similar covariance.

3, if the statement? Does not match extends, the default is? Extends Object.

node<?> node = null;//equivalent to node<? Extends object>

4, the Java generic syntax in the execution period is actually only know is the object type, because cannot obtain the type information during the execution time, the compiler can only examine the type which the compile time sees, thus causes the above mentioned restriction.

18.1.2 use Super and?

1, if B is a subclass of a, and node< a> can be regarded as a node< b>, then called node has inverse degeneration. Java generics do not have contravariance, can I use type wildcard characters? With super to declare that it achieves the effect of a similar inverse degeneration.

2, if the generic class or interface is not covariant or inverse, it is called immutable or rigorous.

18.2 Customizing enumerations 18.2.1 Understanding Java.lang.Enum Classes

1, directly write the program inherits the enum class will be rejected by the compiler program.

2, enum is an abstract class, can not be directly instantiated, it operates the comparable interface. The action's constructor is declared private, so it can only be called in the action class.

3. APIs written before JDK1.4 still use interface to define constants as enumeration values.

4. The Equals () method of the enum and the Hashcode () method basically inherit the behavior of object, but are marked as final. Because the mark is final, the definition enumeration is that the Equals () and hashcode () cannot be re-manipulated because the enumeration member, in the JVM, has a single instance of wisdom, the Equals () of the object definition and the hashcode () The comparison as an object equality is the appropriate definition.

18.2.2 Enum Advanced Application

1, the values () method, the internal maintenance action enumeration instance of the data is returned after replication. You can use this method if you want to know which enumeration members you have.

2. The enum class can define its own constructor, but not the public constructor, nor can it call super () in the constructor.

3, the call constructor in the enum is very special, directly after the enumeration member with parentheses, you can specify the arguments required by the constructor.

4. In the static block, the compiler still maintains the value of name and ordinal itself, followed by the value of value passed in when the custom constructor was called.

5. Specific value class Ontology syntax: After enumerating members, add the Execute () method of the {} operation command directly, which means that each enumeration instance will have a different execute () Cao Zhuo, which is much clearer than switch on the assignment of responsibilities. A particular value class ontology syntax can be used not only when manipulating interfaces, but also in redefining the parent class method.

18.3 about annotations

Use annotations in the source code, provide additional compilation hints to the compiler, or provide configuration information that can be read at the time of application execution. Comments can be used only for the original code, and the. Class document is compiled to be read only by the compiler or during open execution time.

18.3.1 Common Standard Notes

1. @Override
Is the standard comment, the annotated method must be a defined method in the parent class or interface, and compile the program to assist in really redefining the method.

2. @Deprecated
If a method originally existed with the API and is not recommended for reuse later, you can comment on the method. If a user subsequently wants to invoke or redefine this method, the compiler will warn you. For APIs that support generics, it is recommended to explicitly specify a generic true type, and if not specified, the compiler warns.

3. @SuppressWarnings
Specifies the warning that suppresses the unchecked to occur:

@SuppressWarnings (value={"Unchecked"})

4. @SafeVarargs
Indicates that the developer has determined to avoid the heap pollution problem. The problem with heap pollution is that the compiler cannot check the execution period for type errors and cannot specifically confirm the argument type.

5. @FunctionalInterface
Allows the compiler to assist in checking whether interface can be used as the target type of a lambda

18.3.2 Custom Annotation Types

1, marking the comment: is the comment name itself is information, for the compiler or application, the main is to check whether there are comments, and make corresponding actions.

2. Relevant rules:

(1) If the comment name itself cannot provide enough information, set a single-value comment
(2) Note Attributes can also be specified in an array form.
(3) When defining annotation properties, if the property name is value, you can omit the property name and specify the value directly.
(4) Set the default value for the member and use the Defaults keyword.
(5) To set the array default value, you can add {} after default, and you can place the element value in {} if necessary.

3. When defining annotations, you can specify Java.lang.annotation.ElementType enumeration values when you use Java.lang.annotation.Target qualification.

4, in the production of Javadoc files, the default is not to add the comment data into the file, if you want to add the comment data to the file, you can use java.lang.annotation.Documented.

5, the default parent class sets the comment, will not be inherited to the subclass, when defines the annotation, sets the java.lang.annotation.Inherited annotation, may let the annotation quilt class inherit.

18.3.3 JDK8 Callout Enhancements

1. The enumeration member of ElementType is used to qualify which declaration position can be labeled. In JDK8, two enumeration members of type _parameter, type _use are added.

2, Elementtype.type _ use can be used for labeling in various types, a label if it is set to Elementtype.type_use, as long as the type name, can be labeled.

3. @Repeatable
Allows you to repeat the same label in the same place

4. @Filters
As a container for collecting duplicate callout information, each @filters stores its own specified string value.

18.3.4 read comment information during execution

1. Custom annotations, The default is to store the comment information in a. class document, which can be read by the compiler or the Code analysis tool, but cannot read the comment information during the execution period and read the annotation information during the execution time, which can be used with java.lang.annotation.Retention java.lang.annotation.Re Tentionpolicy enumeration specified.

2, Retentionpolicy is the time for runtime, is to let the annotations in the execution period to provide application information, you can use the Java.lang.reflect.AnnotatedElement interface operation object to obtain the annotation information.

3, JDK 8 has added getdeclaredannotation (), Getdeclaredannotationsbytype (), Getannotationsbytype () three methods.
Getdeclaredannotation () allows you to retrieve the specified callout, and when you specify a callout for @repeatable, you look for a container that collects duplicate labels.
Getdeclaredannotationsbytype (), Getannotationsbytype () will not process the @repeatable tag.

Java JDK8 Learning notes--18th. Customizing generics, enumerations, and annotations

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.