Basic concepts of annotations (Annotation) in Java

Source: Internet
Author: User

What is annotation (Annotation):

Annotation (annotation) is Java provides a way and method of associating any information with any metadata (metadata) in the elements of a program. Annotion (annotation) is an interface in which a program can retrieve the Annotion object of a specified program element by reflection, and then obtain the metadata in the annotation by Annotion object.

Annotation (annotations) are introduced in JDK5.0 and later versions. It can be used to create documents, track dependencies in code, and even perform basic compile-time checks. In some ways, annotation is used like a modifier and applies to packages, types, construction methods, methods, member variables, parameters, and declarations of local variables. This information is stored in the annotation "name=value" structure pair.

The members of the annotation are declared in the annotation type in the form of a parameterless method. Its method name and return value define the name and type of the member. Here is a specific default syntax: Allow the default value of any annotation member to be declared: a annotation can name=value to a annotation member that does not have a defined default value, and of course can use Name= value pairs to override other member defaults. This is somewhat similar to the inheritance attribute of the class, which can be the default constructor of a subclass, but can also be overridden by a quilt class.

Annotation can be used to correlate any information to a program element (class, method, member variable, and so on). It should be noted that there is a basic rule: Annotation can not affect the execution of program code, regardless of adding, deleting Annotation, the code is consistent execution. In addition, while some annotation are accessed through the Java Reflection API method at run time, the Java language interpreter ignores these annotation at work. It is because the Java virtual machine ignores the annotation that the annotation type is "non-functional" in the code, and only through some kind of matching tool will the information in the annotation type be accessed and processed. The standard annotation and meta-annotation types are covered in this article, and the tools that accompany these annotation types are the Java compilers (of course they are handled in a special way).

What is metadata (meta data):

The metadata is translated from the word metadata, which means "data on data".

Metadata has a number of functions, such as: You may have used the annotations of Javadoc to automatically generate documents. This is one of the meta data features. In general, metadata can be used to create documents, track the dependencies of code, perform compile-time format checks, and replace existing configuration files. If you want to classify the role of metadata, there is no clear definition, but we can according to its role, can be roughly divided into three categories: P>1. Writing documents: Generating document P>2 with metadata identified in code. Code Analysis: Code Analysis p>3 through the metadata identified in the code. Compile check: The metadata identified in the code allows the compiler to implement basic compilation checks

In Java, metadata exists as a label in Java code, and the existence of metadata labels does not affect the compilation and execution of program code, it is only used to generate other files or a needle at runtime to know the description of the code being run.

To sum up:p> first, metadata exists in the form of a label in Java code. p> second, the metadata describes the information that is type-safe, that is, the fields within the metadata are explicitly typed. P> third, the metadata requires additional processing of tools outside the compiler to generate other program parts. P> Four, the metadata can exist only at the Java source level or within the compiled class file.

Annotation and annotation types:

Annotation:

annotation uses the new syntax that comes with java5.0, which behaves much like public and final modifiers. Each annotation has a >=0 name and number of members. The members of each annotation have names and values called Name=value pairs (just like JavaBean), Name=value loaded with annotation information.

Annotation type:

The annotation type defines the annotation name, type, and member default values. A annotation type can be said to be a special Java interface, its member variables are restricted, and the new syntax is required when declaring annotation types. When we access annotation through the Java Reflection API, the return value will be an object that implements the annotation type interface, and we can easily access its annotation members by accessing the object. The following sections refer to the 3 standard annotation types included in the Java.lang package in java5.0.

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.