`
leonzhx
  • 浏览: 774169 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 24: Eliminate unchecked warnings

阅读更多

1.  When you program with generics, you will see many compiler warnings: unchecked cast warnings, unchecked method invocation warnings, unchecked generic array creation warnings, and unchecked conversion warnings.

 

2.  If you eliminate all warnings, you are assured that your code is typesafe, which means that you won’t get a ClassCastException at runtime.

 

3.  If you can’t eliminate a warning, and you can prove that the code that provoked the warning is typesafe, then (and only then) suppress the warning with an @SuppressWarnings("unchecked") annotation.

 

4.  The SuppressWarnings annotation can be used at any granularity, from an individual local variable declaration to an entire class. Always use the SuppressWarnings annotation on the smallest scope possible.

 

5.  If you find yourself using the SuppressWarnings annotation on a method or constructor that’s more than one line long, you may be able to move it onto a local variable declaration. It is illegal to put a SuppressWarnings annotation on the return statement, because it isn’t a declaration. Instead, declare a local variable to hold the return value and annotate its declaration.

 

6.  Every time you use an @SuppressWarnings("unchecked") annotation, add a comment saying why it’s safe to do so.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics