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 被注释的元素必须为 ...
随机推荐
- Vue 初始化多个Vue 及之间的相互修改
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 9.26 H5日记
9.26 1.新的背景属性,background-position background-position有两个值,水平和垂直,单位px ❤在html和CSS当中,有三个属性可以向服务器发送请求,分别 ...
- ----转载----【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序
关于 chrome 扩展的文章,很久之前也写过一篇.清除页面广告?身为前端,自己做一款简易的chrome扩展吧. 本篇文章重在分享一些制作扩展的过程中比较重要的知识及难点. 什么是 chrome 扩展 ...
- 18.Mysql SQL优化
18.SQL优化18.1 优化SQL语句的一般步骤 18.1.1 通过show status命令了解各种SQL的执行频率show [session|global] status; -- 查看服务器状态 ...
- Informatica_(3)组件
一.Informatica介绍Informatica PowerCenter 是Informatica公司开发的世界级的企业数据集成平台,也是业界领先的ETL工具.Informatica PowerC ...
- 探索未知种族之osg类生物---呼吸分解之事件循环三
那我们就开始处理这些事件中得到的所有的交互事件,首先我们要判断这些事件是否包含osg的退出事件,那什么情况下会触发这个退出事件呢?如果您运行过osg中example中的小例子的,聪明的你一定就会发现当 ...
- 转录组的组装Stingtie和Cufflinks
转录组的组装Stingtie和Cufflinks Posted: 十月 18, 2017 Under: Transcriptomics By Kai no Comments 首先这两款软件都是用 ...
- POJ3254或洛谷1879 Corn Fields
一道状压\(DP\) POJ原题链接 洛谷原题链接 很显然的状压,\(1\)表示种植,\(0\)表示荒废. 将输入直接进行状压,而要满足分配的草场是适合种草的土地,即是分配时的状态中的\(1\),在输 ...
- vue input,textarea失去焦点调用函数方法
<input type="number" class="num" value="1" @blur.prevent="chan ...
- DbUtils类的添加,修改,删除
package cn.jy.demo; import java.sql.Connection;import java.sql.SQLException; import org.apache.commo ...