代码改变世界

[hyddd的FindBugs分析记录][M D DLS] Dead store to local variable

2009-02-16 15:44  hyddd  阅读(7724)  评论(0编辑  收藏  举报

[M D DLS] Dead store to local variable [DLS_DEAD_LOCAL_STORE]

This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used.

Note that Sun's javac compiler often generates dead stores for final local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives.

 

看下面代码:

public static void main(String args[]) throws Exception{
    Object str 
= new Object() ;  //报错处
    str 
= new Object() ;
    System.out.println(str);
}

 

Object str = new Object();是无用的代码,因为在下面有一句str= new Object();,很多语言编译器它都会做优化,比如:去除一些无用的代码以提高效率。JAVA编译器也会做一些优化,但是,Java编译器对上面这段代码却没有做优化(你可以DComplie确认一下),编译后的.class文件还是new了两次,具体什么原因导致它不去做这个优化我还不能确定,我觉得难做这种优化不是借口,起码不应该是Sun的借口。

修改这段代码方法很简单,随便去掉一行new Object();就可以了。