Java —— 爆出警告?如何忽略?(@SuppressWarnings())

本文介绍如何使用@SuppressWarnings注解来消除Java代码中的各种警告,包括未检查的类型警告、泛型使用警告等,并提供了详细的注解参数说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

在Idea或一些代码检查中,会检测我们的代码,并爆出相关警告。但如果代码检测有偏差,如何消除警告呢?

解决

使用@SuppressWarnings()注解。

代码示例

//忽略uncheck类型的警告
@SuppressWarnings("unchecked")

//忽略uncheck、rawtypes类型的警告
@SuppressWarnings("unchecked","rawtypes") 

//忽略所有警告
@SuppressWarnings("all") 

汇总

警告类型官方解释中文翻译
allto suppress all warnings抑制所有警告
boxing to suppress warnings relative to boxing/unboxing operations抑制装箱、拆箱操作时候的警告
castto suppress warnings relative to cast operations抑制映射相关的警告
dep-annto suppress warnings relative to deprecated annotation抑制启用注释的警告
deprecationto suppress warnings relative to deprecation抑制过期方法警告
fallthroughto suppress warnings relative to missing breaks in switch statements抑制在switch中缺失break的警告
finally to suppress warnings relative to finally block that don’t return抑制finally模块没有返回的警告
hidingto suppress warnings relative to locals that hide variable抑制相对于隐藏变量的局部变量的警告
incomplete-switchto suppress warnings relative to missing entries in a switch statement (enum case)忽略没有完整的switch语句
nlsto suppress warnings relative to non-nls string literals忽略非nls格式的字符
nullto suppress warnings relative to null analysis忽略对null的操作
rawtypesto suppress warnings relative to un-specific types when using generics on class params使用generics时忽略没有指定相应的类型
restrictionto suppress warnings relative to usage of discouraged or forbidden references抑制禁止使用劝阻或禁止引用的警告
serialto suppress warnings relative to missing serialVersionUID field for a serializable class忽略在serializable类中没有声明serialVersionUID变量
static-accessto suppress warnings relative to incorrect static access抑制不正确的静态访问方式警告
synthetic-access to suppress warnings relative to unoptimized access from inner classes抑制子类没有按最优方法访问内部类的警告
uncheckedto suppress warnings relative to unchecked operations抑制没有进行类型检查操作的警告
unqualified-field-accessto suppress warnings relative to field access unqualified抑制没有权限访问的域的警告
unusedto suppress warnings relative to unused code抑制没被使用过的代码的警告

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇宙超级无敌程序媛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值