Home >Java >javaTutorial >Detailed explanation of java knowledge points: annotations and meta-annotations
This article brings you relevant knowledge about java, which mainly introduces issues related to annotations and meta-annotations, including a basic introduction to annotations, a basic introduction to Annotation, etc., Let’s take a look at it together, I hope it will be helpful to everyone.
Recommended study: "java Video Tutorial"
Annotation (annotation), also known as metadata (Metadata), is introduced in JDK1.5 and later versions and is used to modify and interpret data information such as packages, classes, methods, properties, constructors, and local variables. It can be used to create documentation, track dependencies in your code, and even perform basic compile-time checks.
Annotations exist in the code with '@annotation name'. According to the number of annotation parameters, we can divide annotations into three categories: mark annotations, single-value annotations, and complete annotations. kind. Like comments, annotations do not affect program logic, but they can be compiled or run and are equivalent to supplementary information embedded in the code.
In addition, you can choose at compile time whether the annotations in the code only exist at the source code level, or it can also appear in class files or runtime (SOURCE/CLASS/ RUNTIME).
In JavaSE, the purpose of using annotations is relatively simple, such as marking obsolete functions, ignoring warnings, etc. Annotations play a more important role in Java EE, such as being used to configure any aspect of the application, replacing the cumbersome code and XML configuration left in the old version of Java EE.
@Override : To limit a method is to override the parent class method , This annotation can only be used for methods
@Deprecated : Used to represent a certain program element ( kind , Methods, etc. ) Outdated
@SuppressWarnings : Suppress compiler warnings
@Override
class Son extends Father{ @Override public void play(){} }
Note:
@Override indicates that the play method of the subclass overrides the play method of the parent class
If there is no written here @Override It will still override the parent class ply
If you write the @Override annotation, the compiler will check whether the method really overrides the parent class Method, if it is indeed rewritten, the compilation will pass. If it is not rewritten, the compilation error will occur.
@Override can only modify methods, not other classes, packages, properties, etc.
@Deprecated
@Deprecated class A{ @Deprecated public static int name = 1; @Deprecated public void play(){ } }
Note:
It’s obsolete, it doesn’t mean it can’t be used, it’s just not recommended, but it can still be used
Can modify methods, classes, fields, Packages, parameters, etc.
Its function is to achieve compatibility and transition between old and new versions
@SuppressWarnings
@SuppressWarnings ("all") public class word{ }
注意:
关于 SuppressWarnings 作用范围是和你放置的位置相关。比如@SuppressWarnings 放置在 main 方法,那么抑制警告的范围就是 main
通过 @SuppressWarnings 的源码可知,其注解目标为类、字段、函数、函数入参、构造函数和函数的局部变量。
关键字 | 解释 |
---|---|
all | 抑制所有警告 |
boxing | 抑制与封装/拆装作业相关的警告 |
cast | 抑制与强制转型作业相关的警告 |
dep-ann | 抑制与淘汰注释相关的警告 |
deprecation | 抑制与淘汰的相关警告 |
fallthrough | 抑制与 switch 陈述式中遗漏 break 相关的警告 |
finally | 抑制与未传回 finally 区块相关的警告 |
hiding | 抑制与隐藏变数的区域变数相关的警告 |
incomplete-switch | 抑制与 switch 陈述式(enum case)中遗漏项目相关的警告 |
javadoc | 抑制与 javadoc 相关的警告 |
nls | 抑制与非 nls 字串文字相关的警告 |
null | 抑制与空值分析相关的警告 |
rawtypes | 抑制与使用 raw 类型相关的警告 |
resource | 抑制与使用 Closeable 类型的资源相关的警告 |
restriction | 抑制与使用不建议或禁止参照相关的警告 |
serial | 抑制与可序列化的类别遗漏 serialVersionUID 栏位相关的警告 |
static-access | 抑制与静态存取不正确相关的警告 |
static-method | Suppress warnings related to methods that may be declared static |
super | Suppress warnings related to substitution methods that do not contain super calls |
synthetic-access | Suppress warnings related to non-optimized access to internal classes |
sync-override | Suppresses override sync methods Missed synchronization warning |
unchecked | Suppress with Warnings related to unchecked jobs |
unqualified-field-access | Suppress warnings related to failed field access |
##unused | Suppress warnings related to unused and deactivated code |
The above is the detailed content of Detailed explanation of java knowledge points: annotations and meta-annotations. For more information, please follow other related articles on the PHP Chinese website!