java注解学习(1)注解的作用和三个常用java内置注解
今天,记录一下自己学习的关于注解方面的知识。
Annotation是从JDK5.0开始引入的新技术
Annotation的作用:
-不是程序本身,可以对程序做出解释(这一点和注释没什么区别)
-可以被其他程序(比如编译器)读取。(注解信息处理流程,是注解和注释的重大区别,如果没有注解信息处理流程,则注解将毫无意义。)
-注解是以 "@注释名" 在代码中存在的,还可以添加一些数值,如: @SuppressWarnings(value="unchecked")。
Annotation在哪里使用:
-可以在package,class,method,field等上面,就如同给它们添加了额外的辅助信息,后面我们可以通过反射机制编程实现对这些元数据的访问。
常用的java内置注解:
1.@Override
-定义在java.lang.Override中,此注释只适用于修饰方法,表示一个方法声明打算重写超类中的另一个声明。
package java.lang; import java.lang.annotation.*; /**
* Indicates that a method declaration is intended to override a
* method declaration in a supertype. If a method is annotated with
* this annotation type compilers are required to generate an error
* message unless at least one of the following conditions hold:
*这段话指明了Overiide的作用,说明Override用来暗示这是一个重写父类的一个方法,如果这个方法没有重写父类的方法编译器就会生成一个错误
* <ul><li>
* The method does override or implement a method declared in a
* supertype.
* </li><li>
* The method has a signature that is override-equivalent to that of
* any public method declared in {@linkplain Object}.
* </li></ul>
*
* @author Peter von der Ahé
* @author Joshua Bloch
* @jls 9.6.1.4 @Override
* @since 1.5
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
@interface是定义注解的关键字,@Target和@Retention也是注解,一个表示定义的注解可以修饰在哪,一个表示定义的注解的生命周期。这里我们知道就好后面再细说。
@Overiide就是用来说明修饰的方法是一个重写父类的方法,如果没有重写父类的方法就会报错。
public class TestAnnotation01 /* extends Object */{
//TestAnnotation01 继承了Object类,这里我们使用注解@Override重写了Object方法,如果我们修改一下toString的名字,编译器就会报错
@Override
public String toString(){
return "";
}
}
2. @Deprecated
-定义在java.lang.Deprecated中,此注释可用于修饰方法、属性。类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择
package java.lang; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; /**
* A program element annotated @Deprecated is one that programmers
* are discouraged from using, typically because it is dangerous,
* or because a better alternative exists. Compilers warn when a
* deprecated program element is used or overridden in non-deprecated code.
* 带注释的程序元素是不鼓励程序员使用的,通常是因为它很危险,或者因为存在更好的替代方法。
* @author Neal Gafter
* @since 1.5
* @jls 9.6.3.6 @Deprecated
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
当然我们硬要使用这个方法也是可以的

3.@SuppressWarnings
-定义在java.lang.SupressWarnings中,用来抑制编译时的警告信息
-与前面两个注解有所不同,需要添加一个参数才能正确使用,这些参数都是定义好的
参数如下:
| 参数 | 含义 |
|---|---|
| deprecation | 使用了过时的类或方法时的警告 |
| unchecked | 执行了未检查的转换时的警告 |
| fallthrough | 当Switch程序块进入进入下一个case而没有Break时的警告 |
| path | 在类路径、源文件路径等有不存在路径时的警告 |
| serial | 当可序列化的类缺少serialVersionUID定义时的警告 |
| finally | 任意finally子句不能正常完成时的警告 |
| all | 以上所有情况的警告 |
package java.lang; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; /**
* Indicates that the named compiler warnings should be suppressed in the
* annotated element (and in all program elements contained in the annotated
* element). Note that the set of warnings suppressed in a given element is
* a superset of the warnings suppressed in all containing elements. For
* example, if you annotate a class to suppress one warning and annotate a
* method to suppress another, both warnings will be suppressed in the method.
* 指示指定的编译器警告应该在带注释的元素(以及带注释的元素中包含的所有程序元素)中被抑制。
注意,在给定元素中抑制的警告集是所有包含元素中抑制的警告的超集。
例如,如果注释一个类来抑制一个警告,注释一个方法来抑制另一个警告,那么这两个警告都会在方法中被抑制。
* <p>As a matter of style, programmers should always use this annotation
* on the most deeply nested element where it is effective. If you want to
* suppress a warning in a particular method, you should annotate that
* method rather than its class.
*
* @author Josh Bloch
* @since 1.5
* @jls 4.8 Raw Types
* @jls 4.12.2 Variables of Reference Type
* @jls 5.1.9 Unchecked Conversion
* @jls 5.5.2 Checked Casts and Unchecked Casts
* @jls 9.6.3.5 @SuppressWarnings
*/
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/**
* The set of warnings that are to be suppressed by the compiler in the
* annotated element. Duplicate names are permitted. The second and
* successive occurrences of a name are ignored. The presence of
* unrecognized warning names is <i>not</i> an error: Compilers must
* ignore any warning names they do not recognize. They are, however,
* free to emit a warning if an annotation contains an unrecognized
* warning name.
*
* <p> The string {@code "unchecked"} is used to suppress
* unchecked warnings. Compiler vendors should document the
* additional warning names they support in conjunction with this
* annotation type. They are encouraged to cooperate to ensure
* that the same names work across multiple compilers.
* @return the set of warnings to be suppressed
*/
String[] value();
}
例如下面

我们在创建List对象没指定存储的对象类型就会警告,现在我们给它加上@SupressWarnings注解。

警告就被抑制了,当然你把@SupressWarnings加在类上面也是可以的。
总结:
以上就是java Annotation的基础内容和三个常用的java内置注解,后面我们开始学习如何自定义注解。
java注解学习(1)注解的作用和三个常用java内置注解的更多相关文章
- java web学习总结(二十五) -------------------JSP中的九个内置对象
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...
- java内置注解、元注解和自定义注解
注解的作用: 1.生成文档 2.跟踪代码依赖性 3.编译时进行格式检查 ---------------------------------------------------------------- ...
- Java注解-元数据、注解分类、内置注解和自定义注解|乐字节
大家好,我是乐字节的小乐,上次说过了Java多态的6大特性|乐字节,接下来我们来看看Java编程里的注解. Java注解有以下几个知识点: 元数据 注解的分类 内置注解 自定义注解 注解处理器 Ser ...
- java注解——内置注解和四种元注解
java内置注解: @Override(重写方法):被用于标注方法,用于说明所标注的方法是重写父类的方法 @Deprecated(过时方法):用于说明所标注元素,因存在安全问题或有更好选择而不鼓励使用 ...
- Java 内置注解简单理解
感谢原文作者:yejg1212 原文链接 https://www.cnblogs.com/yejg1212/p/3187362.html https://www.cnblogs.com/yejg121 ...
- 尚学堂 208.Annotation注解和内置注解
208.Annotation注解和内置注解 override:这个注释的作用是标识某一个方法是否覆盖了它的父类的方法deprecated:表示果某个类成员的提示中出现了个词,就表示这个并不建议使用这个 ...
- 注解_概念和注解_JDK内置注解
注解: 概念:说明程序的,给计算机看的 注解:用文字描述程序的,给程序员看的 定义:注解(Annotation),也叫元数据.一种代码级别的说明.他是JDK1.5及以后的版本引入的一个特性,与类,接口 ...
- JAVA内置注解 基本注解
温故而知新,可以为师矣! 每天复习,或者学习一点小东西,也能水滴石穿! 今天复习5个JAVA内置基本注解(贴代码胜过千言万语): package com.lf.test; import java.ut ...
- hibernate validation内置注解及自定义注解
Bean Validation 中内置的 constraint @Null 被注释的元素必须为 null @NotNull 被注释的元素必须不为 null @AssertTrue 被注释的元素必须为 ...
随机推荐
- Vim完全教程
一.简介 世界上只有三种编辑器,EMACS.VIM和其它. 我们所处的时代是非常幸运的,有越来越多的编辑器,相对于古老的VIM和EMACS,它们被称为现代编辑器.我们来看看这两个古董有多大年纪了: ...
- git 分支强制删除
添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支. 现在,你终于接 ...
- Python 官方文件
7.2. 文件读写 函数 open() 返回 文件对象,通常的用法需要两个参数:open(filename, mode). >>> f = open('workfile', 'w') ...
- 网页 PHP 动态师范
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 洛谷1066 2^k进制数
原题链接 大力猜结论竟然猜对了.. 对于一对\(k,w\),我们可以把\(w\)位划分成\(k\)位一段的形式,每一段就是转换成十进制后的一位,这个从题面的解释中应该可以理解. 先不考虑可能多出(即剩 ...
- Servlet会话管理一(URL重写和表单隐藏域)
会话可以简单的理解为客户端用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器的整个过程称为一个会话.即一个客户端用户和服务器端进行通讯的过程,也是客户端和服务器端之间的数据传 ...
- [RF]怎样用Robot Framework写好Test Case?
1.介绍 这是一个关于如何用Robot Framework写好Test Case的高层次的指导准则 怎样实际的与系统进行交互不在此文档范围内 最重要的准则是使测试用例尽可能的让熟悉此领域的人觉得简单易 ...
- Spring ConversionService 类型转换(一)Converter
Spring ConversionService 类型转换(一)Converter Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.h ...
- [AI]AI章2 框架比较
深度学习框架比较 神经网络一般包括:训练,测试两大阶段.训练:就是把训练数据(原料)和神经网络模型:如AlexNet.RNN等“倒进” 神经网络训练框架例如cafffe等然后用 CPU或GPU(真火) ...
- Minimum Size Subarray Sum LT209
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...