Annotations are one of the fundamental language changes
introduced in Java SE5. They provide information that you need to fully describe your program, but that cannot be expressed in Java. Thus, annotations allow you to
store extra information about your program in a format that is
tested and verified by the compiler.
Annotations can be used to generate descriptor files or even new class definitions and help ease the burden of writing
"boilerplate" code(样板代码). Using annotations, you can keep this metadata in the Java source code, and have the advantage of cleaner looking code, compile-time type checking and the annotation API to help build
processing tools for your annotations. Although a few types of metadata come predefined in Java SE5, in general the kind of annotations you add and what you do with them are entirely up to you.
The syntax of annotations is reasonably simple and consists mainly of the addition of the @ symbol to the language. Java SE5 contains three generalpurpose built-in annotations, defined in java.lang:
?
@Override, to indicate that a method definition is intended to override a method in the base class. This generates a compiler error if you accidentally misspell the method name or give an improper
signature.
?
@Deprecated, to produce a compiler warning if this element is used.
?
@SuppressWarnings, to turn off inappropriate compiler warnings. This annotation is allowed but not supported in earlier releases of Java SE5 (it was ignored).
Syntactically, annotations are used in much the same way as modifiers.
Most of the time, you will be defining your own annotations and writing your own processors to deal with them.
Apart from the @ symbol, the definition of @Test is much like that of an empty interface.
(注解相当于一个空的接口,除了系统自带的注解之外,可以自定义注解)