Java FAQ _03 Basic Grammar (019) _ What is the annotation

Source: Internet
Author: User
Tags deprecated

Click to enter _ more _java thousand ask

1. What are the annotations?

Annotations in Java (Annotation), also called metadata. is a feature introduced in later versions of Java 5.

Annotations and classes, interfaces, enumerations are at the same level, can be used to label packages, classes, fields, methods, local variables, method parameters and other elements to achieve the description and description of these elements.

Annotations can allow the JVM to read it in the run, which is completely different from annotations. and contains a variety of load policies, can be flexibly configured.

See the differences between annotations and comments here: What is the difference between annotations and annotations?
How to customize annotations see here: How to use annotations

2. What are the loading strategies for annotations?

The annotations contain a configurable load policy (Retentionpolicy) in 3, with different configurations depending on the requirements, as follows:

RetentionPolicy {    // 此类型会被编译器丢弃    SOURCE,    // 此类型注解会保留在class文件中,但JVM会忽略它,默认策略    CLASS,    // 此类型注解会保留在class文件中,JVM会读取它    RUNTIME}
3. What is the function of annotations?

The main functions of annotations are as follows:

    1. Writing documents
      A document is generated from the metadata identified in the code, which is similar to a comment.

    2. Code Analysis
      The code is analyzed by the metadata identified in the code, and the annotation information is generally obtained using reflection.

    3. Compile check
      The metadata identified in the code allows the compiler to implement basic compilation checks, such as methods that overwrite @override.

4. The JDK has built-in annotations

The JDK provides several built-in annotations, common to the following:

@Override

It is used to tag a method that overrides a parent class, implements an interface method, and if the tagged method does not actually overwrite the parent class method, the compiler issues an error warning.
Example:

publicclass SuperTest {    publictostring() {        return"父类";    }}publicclass Test extends SuperTest {    @Override    publictostring() {        return"子类注解";    }}

@Deprecated

It is used to mark outdated methods and deprecated methods. For some methods that have expired and are deprecated, but cannot be deleted directly (still used elsewhere), we will use @deprecated for tagging, and when using these methods, we will be prompted at compile time.

Example:

publicclass Test {    publicstaticvoidmain(String[]args) {        // 使用DeprecatedClass里声明被过时的方法        DeprecatedClass.DeprecatedMethod();    }}class DeprecatedClass {    @Deprecated    publicstaticvoidDeprecatedMethod() {    }}

@SuppressWarnings

It is used to mark warnings that you do not want to be prompted, and the warning type can be controlled by parameters, as follows:

Deprecation, warning when an outdated class or method is used
Unchecked, warning when an unchecked conversion was performed
Fallthrough, a warning when a switch block directly leads to the next situation without a break
Path, warning when there are non-existent paths in the Classpath, source file path, and so on
Serial, warning when a serialversionuid definition is missing on a serializable class
Finally, a warning when any finally clause does not complete properly
All, warnings about all of these cases

Example:

publicclass Test {    publicstatic Listlist = newArrayList();    @SuppressWarnings("unchecked")    publicvoidadd(String data) {        list.add(data);    }}

Java FAQ _03 Basic Grammar (019) _ What is the annotation

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.