Home > Java > javaTutorial > What are java annotations?

What are java annotations?

青灯夜游
Release: 2019-12-26 14:28:05
Original
5271 people have browsed it

Since the introduction of annotations in Java 5.0, it has become a very important part of the Java platform. During the development process, we often see annotations such as @Override and @Deprecated in the application code. So what are annotations? Let me introduce it to you below.

What are java annotations?

What are annotations?

Annotations are also called metadata, which is a kind of data that describes data. Annotations are a feature introduced in JDK 1.5. They are used to explain the code. They can annotate packages, classes, interfaces, fields, method parameters, local variables, etc.

What are the uses of annotations?

Official explanation:

Annotations are a series of metadata that provide data to explain the program code, but the annotations are not part of the interpreted code itself. Annotations have no direct impact on the running performance of the code.

1. Provide information to the compiler: The compiler can use annotations to detect errors and warning messages

For example, @Override prompts the subclass to override the modified method in the parent class

2. Processing during the compilation phase: Software tools can be used to use annotation information to generate code, Html documents, or perform other corresponding processing.

3. Runtime processing: Certain annotations can accept code extraction when the program is running.

Annotations are mainly targeted at compilers and other tool software; when developers use Annotation After modifying members such as classes, methods, and fields, these Annotations will not take effect by themselves. The developer must provide corresponding code to extract and process the Annotation information. These codes that handle extracting and processing Annotations are collectively called APT (Annotation Processing Tool).

Annotations can be divided into three categories:

● One type is the standard annotations that come with Java, including @Override (marked to override a certain method), @Deprecated (indicates that a class or method is deprecated) and @SuppressWarnings (indicates warnings to be ignored), the compiler will check after using these annotations.

● One type is meta-annotations. Meta-annotations are annotations used to define annotations, including @Retention (marking the stage at which the annotation is retained), @Target (marking the scope of use of the annotation), and @Inherited (marking the annotation’s usage range). Inheritable), @Documented (indicates whether to generate javadoc documents), @Repeatable.

● One type is custom annotations. You can define annotations according to your own needs.

Meta-annotations

Meta-annotations can be annotated to annotations. Annotation, or meta-annotation, is a basic annotation, but it can be applied to other annotations. To put it more clearly, the annotations we use when defining annotations are meta-annotations.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
}
Copy after login

Except for the @ symbol, annotations are much like an interface. Meta-annotations are required when defining annotations.

There are generally some elements in annotations to represent certain values. Annotated elements look just like interface methods, the only difference is that you can specify a default value for them. Annotations without elements are called mark annotations, and the @Test above is a mark annotation.

The available types of annotations include the following: all basic types, String, Class, enum, Annotation, and array forms of the above types. Elements cannot have undefined values, that is, either have a default value, or provide the element's value when using annotations. And elements cannot use null as the default value. When annotation has only one element and the name of the element is value, you can omit "value=" when using annotation and write the required value directly.

There are 5 types of meta-annotations, @Retention, @Documented, @Target, @Inherited, and @Repeatable.

@Retention

English means retention period. When @Retention is applied to an annotation, it explains how long the annotation will live.

@Documented

This meta-annotation must be related to the document. Its function is to include the elements in the annotation into Javadoc.

@Target

The meaning of the target, @Target specifies the place where the annotation is used

@Inherited

Inherited means inheritance, but it does not mean that the annotation itself can be inherited, but that if a super class is annotated with an @Inherited annotation, then if its subclass is not applied by any annotation If so, then this subclass inherits the superclass annotation

@Repeatable

Repeatable naturally means repeatable. @Repeatable was only added in Java 1.8, so it is considered a new feature.

Recommended learning: Java video tutorial

The above is the detailed content of What are java annotations?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template