A boxed value is unboxed and then immediately reboxed
Solution 1
The problem is that you're converting Long
-> long
-> Long
.
So in the background:
Long.valueOf(lmt)
convertsLong
tolong
emp.setLimit(<long>);
convertslong
toLong
again
As of Java 5 autoboxing happens => your code should look like this:
Employee emp = new Employee()
Long lmt = 123L;
emp.setLimit(lmt);
or even:
Employee emp = new Employee()
long lmt = 123L;
emp.setLimit(lmt);
Solution 2
That happens because Long.valueOf(long)
will unbox your lmt
from Long
to long
, just to get a Long
again. As you said that limit
is a Long
, you don't need to use Long.valueOf
, just use the var:
emp.setLimit(lmt);
Solution 3
emp.setLimit(Long.valueOf(lmt));
Long.valueOf
takes a long
value, but you pass a Long
value -- mandating unboxing. Immediately afterward, however, Long.valueOf
re-boxes the value and the expression evaluates to a Long
again. FindBugs detects the unnecessary conversion chain Long
-> long
-> Long
.
Comments
-
Srinivasan over 3 years
I am getting the Findugs error "A boxed value is unboxed and then immediately reboxed".
This is the Code:
Employee emp = new Employee() Long lmt = 123L; emp.setLimit(Long.valueOf(lmt));
In this, Employee
limit
field is of typeLong
. Could you please let me know what is the error?