FindBugs is giving me a warning about the following line, where invoiceNumber
is an Integer
object:
text.append(String.format("%010d-", (invoiceNumber == null) ? 0 : invoiceNumber));
The warning is: "Boxed value is unboxed and then immediately reboxed"
Now I think I understand (un)boxing, but I can't see how you would do the same thing without getting the warning?
I have found that I can get rid of the warning using the following code instead, but this seems more long-winded:
int invNo = (invoiceNumber == null) ? 0 : invoiceNumber;
text.append(String.format("%010d-", invNo));
Can someone show me what is the 'correct' way to do the above?
BTW, I've looked at the related questions and I understand what was going on with them, but this doesn't seem to match any of those.
text.append(String.format("%010d-", (invoiceNumber == null) ? Ineger.valueOf(0) : invoiceNumber));
will do. – FenestellaInteger
in your original code as well as in your fix withint invNo = (invoiceNumber == null) ? 0 : invoiceNumber;
(though FindBugs warning disappears), but using @Fenestella solution you will not construct the new Integer, but reuse the existing one. Well though Eran answer is correct (upvoted) in general you may just ignore this warning. It's very unlikely that it will actually harm the performance of your application. The created extraInteger
is very short-lived. – Omega