The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
A set of annotation types are predefined in the Java SE API.JavaSEAPI中预定义了一组批注类型。Some annotation types are used by the Java compiler, and some apply to other annotations.有些批注类型由Java编译器使用,有些适用于其他批注。
The predefined annotation types defined in java.lang
are @Deprecated
, @Override
, and @SuppressWarnings
.java.lang
中定义的预定义批注类型为@Deprecated
、@Override
和@SuppressWarnings
。
@Deprecated @Deprecated
annotation indicates that the marked element is deprecated and should no longer be used.批注表示标记的元素已弃用,不应再使用。The compiler generates a warning whenever a program uses a method, class, or field with the 每当程序使用带有@Deprecated
annotation.@Deprecated
批注的方法、类或字段时,编译器都会生成警告。When an element is deprecated, it should also be documented using the Javadoc 当一个元素被弃用时,还应该使用Javadoc @deprecated
tag, as shown in the following example.@deprecated
标记来记录它,如下面的示例所示。The use of the at sign (在Javadoc批注和批注中使用at符号(@
) in both Javadoc comments and in annotations is not coincidental: they are related conceptually.@
)并非巧合:它们在概念上是相关的。Also, note that the Javadoc tag starts with a lowercase d and the annotation starts with an uppercase D.另外,请注意Javadoc标记以小写的d开头,批注以大写的D开头。
// Javadoc comment follows /** * @deprecated * explanation of why it was deprecated */ @Deprecated static void deprecatedMethod() { } }
@Override @Override
annotation informs the compiler that the element is meant to override an element declared in a superclass.批注通知编译器该元素将覆盖在超类中声明的元素。Overriding methods will be discussed in Interfaces and Inheritance.重写方法将在接口和继承中讨论。
// mark method as a superclass method // that has been overridden @Override int overriddenMethod() { }
While it is not required to use this annotation when overriding a method, it helps to prevent errors.虽然重写方法时不需要使用此批注,但它有助于防止错误。If a method marked with 如果标记为@Override
fails to correctly override a method in one of its superclasses, the compiler generates an error.@Override
的方法无法正确重写其超类中的方法,编译器将生成错误。
@SuppressWarnings
@SuppressWarnings
annotation tells the compiler to suppress specific warnings that it would otherwise generate.批注告诉编译器抑制否则将生成的特定警告。In the following example, a deprecated method is used, and the compiler usually generates a warning.在下面的示例中,使用了不推荐使用的方法,编译器通常会生成警告。In this case, however, the annotation causes the warning to be suppressed.但是,在这种情况下,批注会使警告被抑制。
// use a deprecated method and tell // compiler not to generate a warning @SuppressWarnings("deprecation") void useDeprecatedMethod() { // deprecation warning // - suppressed objectOne.deprecatedMethod(); }
Every compiler warning belongs to a category.每个编译器警告都属于一个类别。The Java Language Specification lists two categories: Java语言规范列出了两类:deprecation
and unchecked
.deprecation
和unchecked
。The 当与泛型出现之前编写的遗留代码交互时,可能会出现unchecked
warning can occur when interfacing with legacy code written before the advent of generics.unchecked
的警告。To suppress multiple categories of warnings, use the following syntax:要抑制多个类别的警告,请使用以下语法:
@SuppressWarnings({"unchecked", "deprecation"})
@SafeVarargs
@SafeVarargs
annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on its 当批注应用于方法或构造函数时,断言代码不会对其varargs
parameter.varargs
参数执行潜在的不安全操作。When this annotation type is used, unchecked warnings relating to 使用此批注类型时,将抑制与varargs
usage are suppressed.varargs
用法相关的未选中警告。
@FunctionalInterface
@FunctionalInterface
annotation, introduced in Java SE 8, indicates that the type declaration is intended to be a functional interface, as defined by the Java Language Specification.JavaSE8中引入的批注表示类型声明是由Java语言规范定义的功能接口。
Annotations that apply to other annotations are called meta-annotations.应用于其他批注的批注称为元批注。There are several meta-annotation types defined in java.lang.annotation
.java.lang.annotation
中定义了几种元批注类型。
@Retention
@Retention
annotation specifies how the marked annotation is stored:批注指定如何存储标记的批注:
RetentionPolicy.SOURCE
– RetentionPolicy.CLASS
– RetentionPolicy.RUNTIME
– @Documented
@Documented
annotation indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool.annotation指出,无论何时使用指定的批注,都应该使用Javadoc工具记录这些元素。(By default, annotations are not included in Javadoc.)(默认情况下,批注不包括在Javadoc中。)For more information, see the Javadoc tools page.有关更多信息,请参阅Javadoc工具页面。
@Target
@Target
annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to.批注标记另一个批注,以限制该批注可以应用于哪种Java元素。A target annotation specifies one of the following element types as its value:目标批注指定以下元素类型之一作为其值:
ElementType.ANNOTATION_TYPE
ElementType.CONSTRUCTOR
ElementType.FIELD
ElementType.LOCAL_VARIABLE
ElementType.METHOD
ElementType.PACKAGE
ElementType.PARAMETER
ElementType.TYPE
@Inherited
@Inherited
annotation indicates that the annotation type can be inherited from the super class.annotation表示可以从超类继承批注类型。(This is not true by default.)(默认情况下不是这样。)When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type.当用户查询批注类型而类没有此类型的批注时,将为批注类型查询类的超类。This annotation applies only to class declarations.此批注仅适用于类声明。
@Repeatable
@Repeatable
annotation, introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use.JavaSE8中引入的批注表示标记的批注可以多次应用于相同的声明或类型使用。For more information, see Repeating Annotations.有关详细信息,请参阅重复批注。