Java 简明教程

Java - Enhanced @Deprecated Annotation

@Deprecated 注释在 java 5 版本中引入。用 @Deprecated 注释的程序元素表示不应使用该元素,原因如下 -

@Deprecated annotation was introduced in java 5 version. A program element annotated with @Deprecated means it should not be used for any of the following reasons −

  1. Its usage may leads to errors.

  2. It may be incompatible in future version.

  3. It may be removed in future version.

  4. A better and efficient alternative has superseeded it.

每当使用已过期的元素时,编译器都会生成警告。使用 Java 9,会在 @Deprecated 注释中进行两项新增强。

Compiler generates warnings whenever a deprecated element is used. With Java 9, two new enhancements are made to @Deprecated annotation.

  1. forRemoval − Indicates whether the annotated element is subject to removal in a future version. The default value is false.

  2. since − Returns the version in which the annotated element became deprecated. The default value is the empty string.

Deprecated with since

以下 Java 9 中 Boolean 类 javadoc 的示例说明了如何在 @Deprecated 注释中使用 since 属性。

Following example of Boolean class javadoc on Java 9 illustrate the use of since attribute on @Deprecated annotation.

boolean

Deprecated with forRemoval

以下 Java 9 中 System 类 javadoc 的示例说明了如何在 @Deprecated 注释中使用 forRemoval 属性。

Following example of System class javadoc on Java 9 illustrate the use of forRemoval attribute on @Deprecated annotation.

system