Auto Boxing Error

FindBugs tells me that I have the following error:

The primitive is placed in the box, and then immediately unpacked. This is probably due to manual boxing in the place where the unboxed value is required, which forces the compiler to immediately cancel the box.

Here is the relevant code:

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

What does it mean and how to fix it?

+4
source share
3 answers

Looks like a bug in FindBugs for me. If you compile this code and then run javap -c , it never calls doubleValue() , which is usually used for unpacking.

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

I suggest you inform the FindBugs team about this.

EDIT: Before reporting this to the FindBugs team, I update your question with a short but complete program that demonstrates the problem. I took your word for the fact that the code that you showed us is the code referenced by FindBugs. If this is not the case, all bets are disabled :)

+5
source

I tried my code - FindBugs does not display any errors. I think this code is significantly different from what causes the error.

+3
source

You do not need automatic boxing or unpacking.

 double d = str == null ? 0.0 : Double.parseDouble(str); 

The moral is not to use an object if you want to use a primitive.

IMHO Its less confusing to use a positive expression instead of negative and double negative Boolean expressions.

0
source

All Articles