FindBugs 告诉我有以下错误:
A primitive is boxed, and then immediately unboxed. This probably is due to a manual boxing in a place where an unboxed value is required, thus forcing the compiler to immediately undo the work of the boxing.
相关代码如下:
...
String str= "10.0";
Double d = (str != null ? Double.valueOf(str) : new Double(0.0));
...
这是什么意思,我该如何解决?
最佳答案
在我看来,这像是 FindBugs 中的错误。如果您编译该代码然后对其运行 javap -c
,它永远不会调用通常用于拆箱的 doubleValue()
。
无可否认,您可能希望将缓存的 Double
用于零,而不是每次执行时都分配一个,但除此之外它对我来说看起来很合理...
我建议您将此报告给 FindBugs 团队。
编辑:在向 FindBugs 团队报告此问题之前,我会用一个简短但完整 的程序来更新您的问题,该程序演示了该问题。我相信你的话,你向我们展示的代码就是 FindBugs 提示的代码。如果不是这种情况,则所有赌注都将取消 :)
关于java - 自动装箱错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6502216/