Recently, I have learned some basic knowledge of annotations. Now I make the following notes. Please correct me if there is any error.
1. Introduction
1. Understanding
Annotation is to save all information at the actual source code level, which serves as a mark for special functions, rather than a kind of commentary text.
2. Advantages
By using annotations, you can save metadata in java source code, and use annotationAPI to construct processing tools for your own annotations.
a. Can provide a complete description of the information required by the program (when creating a descriptive class or interface, once it contains repetitive work, you can consider using annotations to simplify);
b. Using annotations, the code becomes cleaner and easier Read;
c. Compile-time type checking is possible.
3. Meta-annotations When
defining annotations, some meta-annotations are needed. Java has built-in three standard annotations and four meta-annotations.
Three standard annotations, defined in java.lang:
four meta-annotations
2. Basic usage
1. Syntax
Except for the @ symbol, the definition of annotations is the same as that of java interface (the difference between the two, annotations can be assigned default values for them);
annotations can work together with any modifiers on methods;
2. mark annotations ( No element annotation)
3. Annotation with element
Define the definition of the similar method of the element. As shown in the figure above, the compiler will type-check the id. When the description is not passed in, the annotator of the annotation will use the default value of the element.
2.
Annotated elements are in the form of name-value pairs when used, and need to be in parentheses after the @ Note name declaration, as shown in the following figure: