Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoboxing error

Tags:

java

findbugs

FindBugs is telling me I have the following error:

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.

Here's the relevant code:

...
String str= "10.0";
Double d = (str != null ? Double.valueOf(str) : new Double(0.0));
...

What does this mean, and how do I fix it?

like image 480
Srinivasan Avatar asked Feb 24 '23 18:02

Srinivasan


2 Answers

Looks like a bug in FindBugs to me. If you compile that code and then run javap -c on it, it never calls doubleValue() which is what normally gets used for unboxing.

Admittedly you might want to use a cached Double for zero, rather than allocating one every time this is executed, but other than that it looks reasonable to me...

I suggest you report this to the FindBugs team.

EDIT: Before reporting this to the FindBugs team, I'd update your question with a short but complete program which demonstrates the problem. I took your word for it that the code you showed us was the code FindBugs was complaining about. If that's not the case, all bets are off :)

like image 111
Jon Skeet Avatar answered Feb 26 '23 08:02

Jon Skeet


I tryed your code - FindBugs doesn't display any error. I think that this code significally different from that produces error.

like image 39
Lorenzo Manucci Avatar answered Feb 26 '23 08:02

Lorenzo Manucci