简体   繁体   English

@SuppressWarnings注释的优点

[英]Advantage of @SuppressWarnings annotation

@SuppressWarnings annotation is for cleaner code or by adding it is there any performance gain or any advantage ? @SuppressWarnings注释是为了更清晰的代码,还是通过添加它是否有任何性能提升或任何优势?

or can we reduce compile time by doing so. 或者我们可以通过这样做来减少编译时间。

The @SuppressWarnings annotation type allows Java programmers to disable compilation warnings for a certain part of a program (type, field, method, parameter, constructor, and local variable). @SuppressWarnings注释类型允许Java程序员禁用程序某个部分(类型,字段,方法,参数,构造函数和局部变量)的编译警告。 Normally warnings are good. 通常警告很好。 However in some cases they would be inappropriate and annoying. 但是在某些情况下,它们会不恰当和烦人。 So programmers can choose to tell the compiler ignoring such warnings if needed. 因此,如果需要,程序员可以选择告诉编译器忽略这些警告。

There is no relation with performance. 与表现无关。

The developer who wrote the code knew that it was always going to be safe, decided to use @SuppressWarnings("unchecked") to suppress the warning at compilation. 编写代码的开发人员知道它总是安全的,决定使用@SuppressWarnings(“unchecked”)来抑制编译时的警告。

As mentioned by others, it is just a trust between the developer and code written. 正如其他人所提到的,它只是开发人员和编写的代码之间的信任。

more info here and here 更多信息在这里这里

SuppressWarnings annotation is just to suppress warnings that you know are sure to occur and you don't care about it. SuppressWarnings注释只是为了抑制你知道肯定会发生的警告而你并不关心它。 Its just helps you to have a cleaner inspection, suppressing warnings you expect to see. 它只是帮助您进行更清洁的检查,抑制您希望看到的警告。 No performance gain. 没有性能提升。

There is no performance gain, only when compiling, compiler will or will not write a waring. 没有性能增益,只有在编译时,编译器才会或不会编写waring。 However after compilation, there is no difference whatsoever. 但是在编译之后,没有任何区别。

It does not make your code cleaner or improve the performance. 它不会使您的代码更清晰或提高性能。 It just helps you to concentrate your attention on potentially dangerous code. 它只是帮助您将注意力集中在潜在危险的代码上。 If you have a list of 130 warnings you will soon stop to read them. 如果您有130个警告列表,您很快就会停下来阅读它们。

Most warnings represents bad programming practices or potencial problems that the compiler is not able to solve. 大多数警告表示编译器无法解决的错误编程实践或潜在问题。 A finished program should, ideally, compile with no warnings. 理想情况下,完成的程序应该在没有警告的情况下编译。 This way, when you modify it and a new warning appears you can decide what to do. 这样,当您修改它并出现新警告时,您可以决定该怎么做。

For example: 例如:

  • Unreachable code. 无法访问的代码。 What was I thinking here 我在这里想什么
  • Lib ZZZ is deprecated. Lib ZZZ已被弃用。 Should I upgrade to the new one? 我应该升级到新的吗? Can I continue with this for now? 我现在可以继续吗?
  • Add type arguments to list... ups, I should be using generics 添加类型参数列表... ups,我应该使用泛型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM