Findbugs issue with "Boxing/unboxing to parse a primitive" with Integer.valueOf(String)
The issue is that Integer.valueOf
returns an Integer
, not an int
, but your someOtherMethod
expects an int
. Findbugs is basically warning you that you're doing it a long-winded way that involves potentially creating an object (the Integer
) that you don't need which you're then immediately going to unbox by passing it to someOtherMethod(int)
, e.g.:
String => int => Integer => int ^^^^^^^^^^^^^^ \--- This is inside Integer.valueOf
Instead, you can and probably should avoid that unnecessary round-trip through Integer
and simply do:
String => int^^^^^^^^^^^^^ \--- Integer.parseInt
There's just no need for the temporary Integer
and the potential memory allocation and such surrounding it.
If someOtherMethod
were expecting an Integer
, you wouldn't get the warning, because the Integer
isn't purely temporary.
This is just one of a class of unnecessary-boxing-conversions that Findbugs and tools like it helpfully point out.
It took me a while to figure that one out (partially because Jenkins just said "Boxing/unboxing to parse a primitive"), but apparently the problem / solution is in what Integer.valueOf() does internally, namely:
Integer.valueOf(parseInt(s, 10));
So the solution is to simply to call parseInt() directly instead:
someOtherMethod(Integer.parseInt(id));
The detailed description of the issue (DM_BOXED_PRIMITIVE_FOR_PARSING) can be found on the findbugs page.