今天,记录一下自己学习的关于注解方面的知识。

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&eacute;
* @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内置注解的更多相关文章

  1. java web学习总结(二十五) -------------------JSP中的九个内置对象

    一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...

  2. java内置注解、元注解和自定义注解

    注解的作用: 1.生成文档 2.跟踪代码依赖性 3.编译时进行格式检查 ---------------------------------------------------------------- ...

  3. Java注解-元数据、注解分类、内置注解和自定义注解|乐字节

    大家好,我是乐字节的小乐,上次说过了Java多态的6大特性|乐字节,接下来我们来看看Java编程里的注解. Java注解有以下几个知识点: 元数据 注解的分类 内置注解 自定义注解 注解处理器 Ser ...

  4. java注解——内置注解和四种元注解

    java内置注解: @Override(重写方法):被用于标注方法,用于说明所标注的方法是重写父类的方法 @Deprecated(过时方法):用于说明所标注元素,因存在安全问题或有更好选择而不鼓励使用 ...

  5. Java 内置注解简单理解

    感谢原文作者:yejg1212 原文链接 https://www.cnblogs.com/yejg1212/p/3187362.html https://www.cnblogs.com/yejg121 ...

  6. 尚学堂 208.Annotation注解和内置注解

    208.Annotation注解和内置注解 override:这个注释的作用是标识某一个方法是否覆盖了它的父类的方法deprecated:表示果某个类成员的提示中出现了个词,就表示这个并不建议使用这个 ...

  7. 注解_概念和注解_JDK内置注解

    注解: 概念:说明程序的,给计算机看的 注解:用文字描述程序的,给程序员看的 定义:注解(Annotation),也叫元数据.一种代码级别的说明.他是JDK1.5及以后的版本引入的一个特性,与类,接口 ...

  8. JAVA内置注解 基本注解

    温故而知新,可以为师矣! 每天复习,或者学习一点小东西,也能水滴石穿! 今天复习5个JAVA内置基本注解(贴代码胜过千言万语): package com.lf.test; import java.ut ...

  9. hibernate validation内置注解及自定义注解

    Bean Validation 中内置的 constraint @Null 被注释的元素必须为 null @NotNull 被注释的元素必须不为 null @AssertTrue 被注释的元素必须为 ...

随机推荐

  1. PAT 甲级 1015 Reversible Primes(20)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  2. 前端面试问题css汇总

    1,行内元素有哪些?块级元素有哪些?空元素有哪些?CSS的盒模型? 块级元素:div p h1 h2 h3 h4 form ul li 行内元素: a b br i span input select ...

  3. Ubuntu几种常见乱码解决方法

    一.网页中的flash乱码:        ubuntu默认浏览器是Firefox,但是Ubuntu默认不安装像flash这种带版权的软件,所以当你浏览像youku或网页播放器时,这种带有 flash ...

  4. oracle 视图带参数

    -- create or replace package p_view_param is --参数一 function set_ID(num number) return number; functi ...

  5. C# 使用 HttpPost 请求调用 WebService

    之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService.这里还想说一句,还是 web api 的调用 ...

  6. 一窥kbmmw中的 smart service

    在kbmmw 的新版中(还没有发布),将会有一个叫做smart service 的服务.这种服务的属性基于服务器端,并且可以自动注册服务名,下面就是一个简单例子代码.这个服务里面有有三个发布的函数:e ...

  7. 还一道区间DP -- MZOJ 1346: 不老的传说

    http://10.37.2.111/problem.php?id=1346 与上一道染色基本一样,就加了个限制条件(一次最多刷maxd) #include <bits/stdc++.h> ...

  8. 811. Subdomain Visit Count

    这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...

  9. 2018.11.24 poj3415Common Substrings(后缀数组+单调栈)

    传送门 常数实在压不下来(蒟蒻开O(3)都过不了). 但有正确性233. 首先肯定得把两个字符串接在一起. 相当于heightheightheight数组被height<kheight<k ...

  10. 2018.11.06 洛谷P1941 飞扬的小鸟(背包)

    传送门 上升看成完全背包. 下降看成01背包. 注意边界转移就行了. 代码: #include<bits/stdc++.h> using namespace std; inline int ...