Note

  1. Eliminate every unchecked warning that you can.

    Set<Lark> exaltation = new HashSet();

    The compiler will gently remind you what you did wrong:

    Venery.java:4: warning: [unchecked] unchecked conversion

    found : HashSet, required: Set<Lark>

    Set<Lark> exaltation = new HashSet();

    ^

    You can then make the indicated correction, causing the warning to disappear:

    Set<Lark> exaltation = new HashSet<Lark>();

  2. If you can't eliminate a warning, and you can prove that the code that provoked the warning is type safe, then (and only then) suppress the warning with an @SuppressWarnings("unchecked") annotation.
  3. Always use the Suppress Warnings annotation on the smallest scope possible.

    // Adding local variable to reduce scope of @SuppressWarnings

    public <T> T[] toArray(T[] a) {

    if (a.length < size) {

    // This cast is correct because the array we're creating

    // is of the same type as the one passed in, which is T[].

    @SuppressWarnings("unchecked") T[] result =

    (T[]) Arrays.copyOf(elements, size, a.getClass());

    return result;

    }

    System.arraycopy(elements, 0, a, 0, size);

    if (a.length > size)

    a[size] = null;

    return a;

    }

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

Summary

Unchecked warnings are important. Don't ignore them. Every unchecked warning represents the potential for a ClassCastException at run-time. Do your best to eliminate these warnings. If you can't eliminate an unchecked warning and you can prove that the code that provoked it is typesafe, suppress the warning with an @SuppressWarnings("unchecked") annotation in the narrowest possible scope. Record the rationale for your decision to suppress the warning in a comment.

Effective Java 24 Eliminate unchecked warnings的更多相关文章

  1. Effective Java 06 Eliminate obsolete object references

    NOTE Nulling out object references should be the exception rather than the norm. Another common sour ...

  2. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  3. 《Effective Java》读书笔记 - 5.泛型

    Chapter 5 Generics Item 23: Don't use raw types in new code 虽然你可以把一个List<String>传给一个List类型(raw ...

  4. Effective Java 目录

    <Effective Java>目录摘抄. 我知道这看起来很糟糕.当下,自己缺少实际操作,只能暂时摘抄下目录.随着,实践的增多,慢慢填充更多的示例. Chapter 2 Creating ...

  5. 【Effective Java】阅读

    Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...

  6. Effective Java 第三版——24. 优先考虑静态成员类

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  7. Effective Java通俗理解(持续更新)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

  8. Effective Java 第三版——29. 优先考虑泛型

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  9. Effective Java通俗理解(上)

    这篇博客是Java经典书籍<Effective Java(第二版)>的读书笔记,此书共有78条关于编写高质量Java代码的建议,我会试着逐一对其进行更为通俗易懂地讲解,故此篇博客的更新大约 ...

随机推荐

  1. css3照片墙+曲线阴影

    css3照片墙+曲线阴影 最近在学习jquery,晚上想复习下以前学过的知识,看到网上有关于css3照片墙的,感觉挺好玩的,就做了做.(以下图片均来自网络) 一.css3照片墙 html部分: < ...

  2. CentOS6.5菜鸟之旅:VirtualBox4.3识别USB设备

    一.前言 VirtualBox默认是不能识别USB设备的,但可以通过Oracle VM VirtualBox Extension Pack来增强VirtualBox的功能,增强的功能如下: 1. US ...

  3. JS 的 call apply bind 方法

    js的call apply bind 方法都很常见,目的都是为了改变某个方法的执行环境(context) call call([thisObj[,arg1[, arg2[,   [,.argN]]]] ...

  4. 2015 Multi-University Training Contest 1 - 10010 Y sequence

    Y sequence Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Mean: 有连续数列A={1,2,3,4,5,6, ...

  5. 设计模式--适配器(Adapter)模式

    今天学习另一个设计模式,适配器(Adapter)模式,这是一个共同方向,但有特殊要求,就应用到此设计模式.写到这里,想起很久以前,有写过一篇<ASP.NET的适配器设计模式(Adapter)&g ...

  6. 3. Node.js REPL(交互式解释器)

    1. 双击安装完成的Node.js 或者在 cmd 中 执行"node"  可以启动node 的终端. 2. 在node终端中可以输入一些javascript语法, 例如: > ...

  7. 将表数据生成Insert脚本

    set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author ...

  8. jsp iframe example

    1. jsp中用iframe的方式在body中展示列表, 可以通过父元素的宽.高来设定iframe的宽高. <div class="wrapper" style=" ...

  9. 【Asphyre引擎】Asphyre时隔3年,更名为PXL,全平台支持!

    ps:回忆日志 新版本10月初就推出了,我第一时间(10.2日更新,我当天就看到了)下载下来.发现部分Demo需要XE8才能编译通过,又去下载了一个XE8.折腾完已经深夜,只是粗粗的把Demo都编译了 ...

  10. Erlang 内存泄漏分析

    随着项目越来越依赖Erlang,碰到的问题也随之增加.前段时间线上系统碰到内存高消耗问题,记录一下troubleshooting的分析过程.线上系统用的是Erlang R16B02版本. 问题描述 有 ...