问题描述
我不明白Java的这个特性。我知道它使编码更容易,有时看起来更整洁,但实际使用的是什么?相反,我觉得,最好显示警告,因为将来任何人都可以在修改代码之前引用它们。这个@SuppressWarnings是否会提高压缩效率,还是根据任何编码标准?
I didn't understand this feature of Java. I know it makes coding easier and sometimes looks neater, but what is the actual use of this? On contrary i feel, its better to display the warnings, as in future any one can refer them before making modifications to code. Does this @SuppressWarnings increases compliling efficiency OR is this according to any coding standards?
推荐答案
其他答案已经解释了 @SuppressWarnings
的用例,但是我想强调一点,有时你绝对需要使用 @SuppressWarnings
来克服语言本身的限制,并且在这些情况下使用 @SuppressWarnings
绝对合法。
Other answers already explained use cases of @SuppressWarnings
a lot, but I want to emphasize the point that sometimes you absolutely need to use @SuppressWarnings
to overcome limitations of the language itself, and in these cases use of @SuppressWarnings
is absolutely legal.
在其他情况下,使用 @SuppressWarnings
可能会被认为是有问题的,因为在这些情况下你可以随时摆脱通过更改代码发出警告(但显然,它并不总是可以接受)。
In other cases use of @SuppressWarnings
can be considered questionable, because in these cases you can always get rid of warnings by changing the code (though, obviously, it's not always acceptable).
以下是一些常见情况,如果没有 @SuppressWarnings :
Here are some common cases when you absolutely cannot get rid of warnings without @SuppressWarnings
:
- 实现支持数组的通用集合(因为您无法创建通用数组)
- 一个
Map
从类到它们的实现(因为你不能将泛型类型参数的不同值分配给不同的地图)
- Implementing an array-backed generic collection (since you cannot create a generic array)
- A
Map
from classes to their implementations (since you cannot assign different values of generic type parameter to different map entires)
这篇关于@SuppressWarnings有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!