Starting from JDK5, Java has added support for metadata, that is, annotations. There is a certain difference between annotations and annotations. Annotations can be understood as special marks in the code. These marks can be used during compilation, class loading, and runtime. is read and corresponding processing is performed. Through annotations, developers can embed supplementary information in the source code without changing the original code and logic.
What are annotations
Annotations can be regarded as an extended template for a class/method. Each class /Method annotates different parameters for the class/method according to the rules in the annotation class. Where used, you can get the various parameters and values annotated in different classes/methods
Annotation is Annotation. I believe that many people, like me before, think that comments are a piece of auxiliary text just like comments and docs. In fact, comments are not like this.
Starting from JDK5, java has added support for metadata (information describing data attributes). In fact, to put it bluntly, they are special flags in the code. These flags can be read during compilation, class loading, and runtime, and corresponding processing can be performed to facilitate other tools to supplement information or deploy.
Annotation (Annotation) It can be used to create documentation, track dependencies in your code, and even perform basic compile-time checks. In some ways, annotations are used like modifiers and are applied to the declaration of packages, types, constructors, methods, member variables, parameters, and local variables. This information is stored in the "name=value" structure pair of Annotation.
Members of Annotation are declared in the form of parameterless methods in the Annotation type. Its method name and return value define the name and type of the member.
There is a specific default syntax here:
Allows the declaration of a default value for any Annotation member: an Annotation can have a name=value pair as if no default value is defined The value of the Annotation member, of course, can also use name=value pairs to override the default values of other members. This is somewhat similar to the inheritance characteristics of classes. The constructor of the parent class can be used as the default constructor of the subclass, but it can also be overridden by the subclass.
Annotation can be used to associate any information with a certain program element (class, method, member variable, etc.).
It should be noted that There is a basic rule here:
Annotation cannot affect the execution of the program code. No matter whether annotation is added or deleted, the code will always be the same. implement.
In addition, although some annotations are accessed at runtime through Java's reflection API methods, the Java language interpreter ignores these annotations when working. It is precisely because the Java virtual machine ignores Annotation that the annotation type "does not work" in the code;
The information in the annotation type can only be accessed and processed through some supporting tools. .
The meaning of annotations
Provides auxiliary information to the compiler - Annotations can provide external information to the compiler to facilitate detection of errors, suppression of warnings, etc.
Compile source code Time-out operation - software tools can process Annotation information to generate original code, xml files, etc.
Run-time processing - Some annotations can even be detected while the program is running, using.
In short, annotations It is a kind of metadata, which plays the role of "description and configuration".
The above is the detailed content of Why are there annotations in java classes?. For more information, please follow other related articles on the PHP Chinese website!