Java Basics-Annotations

Source: Internet
Author: User

I. Overview

 Quoted from Baidu Encyclopedia:

Definition: Annotations (Annotation), also called metadata. A description of the code level. It is a feature introduced in JDK1.5 and later versions, with classes, interfaces, and enumerations at the same level. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, and so on, to explain these elements, comments.

Second, JDK built-in annotations

 Java provides three built-in annotations.

 1. @Override-when we want to replicate a method in a parent class, we need to use that note to tell the compiler that we want to replicate this method. This allows the compiler to prompt for an error message when a method in the parent class is removed or a change occurs.

2. @Deprecated-We should use this annotation when we want the compiler to know that a method is not recommended for use. Java recommends the use of this annotation in Javadoc, and we should provide an alternative to why the method is not recommended for use.

3. @SuppressWarnings-This simply tells the compiler to ignore specific warning messages, such as using native data types in generics. Its retention policy is source (the translator notes: Valid in the source file) and is discarded by the compiler.

Three, Yuan annotations

  Here are four types of meta-annotations:

1. @Documented -Indicates that the element that owns this annotation can be documented by Javadoc such a tool. This type should be used to annotate types that affect customers ' use of annotated element declarations. If a declaration is annotated with documented , this type of annotation is used as the public API for the annotated program member.

2. @Target-Indicates the scope of the program element to which the annotation of that type can be annotated. The value of this meta-annotation can be type,method,constructor,field and so on. If the Target element annotations do not appear, then the defined annotations can be applied to any element of the program.

3. @Inherited--Indicates that the annotation type is automatically inherited. If the user queries the meta-annotation type in the current class and the declaration of the current class does not contain the meta-annotation type, the current class's parent class is automatically queried for the existence of the inherited element annotation, which is repeated to know that the label type is found, Or a query to the top-level parent class.

4. @Retention-Indicates the length of time that the annotation was retained. The Retentionpolicy value is source,class,runtime.

  more detailed introduction and explanation, see (recommended): http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html (including annotation processor, etc., recommended!) )

Iv. Custom Annotations

As before, we found an already existing annotation to see how it was written (e.g.@SuppressWarnings)

  1. Definition: You can see the use of @interface to declare an annotation

 public @interface suppresswarnings {

  2. Members: note the format of the definition, with parentheses, if necessary, you can follow the default "" Form

String[] Value ();

for full data type support, see the link above

You can also see in the annotations that are used later that if the annotation has only one member and is value, it can be omitted without writing @requestmapping (value = "/add"), which can be abbreviated as ("/add")

Java Basics-Annotations

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.