被忽略的错误——这个我一直都是这么写的啊!

1、".equals()" should not be used to test the values of “Atomic” classes.

不要使用equals方法对AtomicXXX进行是否相等的判断
Object的equals()描述:只有当x和y指向相同的引用时,equals()才返回true。

2、"=+" should not be used instead of “+=”

a =+ b;虽然正确但写法不合规,应写成 a = +b;

3、"@NonNull" values should not be set to null

标注非空假定非空且在使用之前不进行非空检查,设置为空会导致空指针异常

4、“BigDecimal(double)” should not be used

因为浮点的不精确,可能使用BigDecimal(double)得不到期望的值

5、“compareTo” results should not be checked for specific values

compareTo可能返回不是具体的值(除0外),建议用 >0、<0、=0

6、“compareTo” should not return “Integer.MIN_VALUE”

compareTo只代表一个不等标识,不代表不等的程度,应返回-1,0,1标识即可

7、 “Double.longBitsToDouble” should not be used for “int”

Double.longBitsToDouble返回给定的位所代表的double值,需要一个64位的long类型参数.

8、 “equals” method overrides should accept “Object” parameters

equals作为方法名应该仅用于重写Object.equals(Object)来避免混乱.

9、 “equals(Object obj)” should test argument type

要比较obj的class type是否一样

10、“equals” methods should be symmetric and work for subclasses

equals应是对等并且在有子类参与时能正常工作

11、“equals(Object obj)” and “hashCode()” should be overridden in pairs

成对重写

12、“Externalizable” classes should have no-arguments constructors

Externalizable(可序列化与返序列化)类应该有无参构造器

13、“getClass” should not be used for synchronization

{synchronized (this.getClass())} 错误 子类继承此方法时不能做到同步
{synchronized (MyClass.class)} 正确

14、“hashCode” and “toString” should not be called on array instances

使用Arrays.toString(args)和Arrays.hashCode(args)代替.

15、“instanceof” operators that always return “true” or “false” should be removed

16、“InterruptedException” should not be ignored

在代码中不应该忽略中断异常,只在日志中打印异常日志,就像“忽略”一样。抛出中断异常会清除线程的中断状态,因此如果异常处理不当,那么线程被中断的信息将会丢失。相反,中断应该被重新抛出——立即或者在清理方法的状态之后——或者应该通过调用Thread.interrupt()来重新中断线程,即使这是单线程的应用程序。任何其他的操作过程都有延迟线程关闭的风险,并且丢失了线程被中断的信息——线程很可能没有完成它的任务。

类似地,也应该传播ThreadDeath异常。根据它的JavaDoc:

如果ThreadDeath异常被一个方法捕获,那么它被重新抛出是很重要的,这样线程就会结束。

不符合要求的代码如下:

public void run () {
  try {
    while (true) {
      // do stuff
    }
  }catch (InterruptedException e) { // Noncompliant; logging is not enough
    LOGGER.log(Level.WARN, "Interrupted!", e);
  }
}

catch块中只是打印了异常日志,相当于忽略了这个异常。

处理建议为抛出异常:

public void run () {
  try {
    while (true) {
      // do stuff
    }
  }catch (InterruptedException e) {
    LOGGER.log(Level.WARN, "Interrupted!", e);
    // Restore interrupted state...
    Thread.currentThread().interrupt();
  }
}

17、“Iterator.hasNext()” should not call “Iterator.next()”

18、“Iterator.next()” methods should throw “NoSuchElementException”

   public String next(){
    if(!hasNext()){
      throw new NoSuchElementException();
    }
    ...
  }

19、“notifyAll” should be used

notify可能不能唤醒正确的线程,notifyAll代之。

20、“null” should not be used with “Optional”

把判空包装起来使用而不直接使用!=null

21、具有公有地静态final数组域

public static final String[] names={ "zhangsan","lisi"};  

初看这段代码有什么感想?因为names定义的是static final,即定义为"不可变",值得注意的是它只是说明指向该对象的指针不变,对于常量对象来说这点是成立的,但对于数组来说就不成立!
因为上面只能保证names对象不变(意思是说final只能保证基本数据类型或原始数据类型不可变,比如:String,Double),但CODES引用的对象是可以改变的。
解决方案:定义一个私有数组,以及编写一个公有的非可变列表:

private static final String[] tempName={ "A","B", "C", "D", "E"};  
public static final List names= Collections.unmodifiableList(Arrays.asList(tempName));   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值