Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A boxed value is unboxed and then immediately reboxed

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 type Long. Could you please let me know what is the error?

like image 775
Srinivasan Avatar asked Aug 22 '12 05:08

Srinivasan


2 Answers

The problem is that you're converting Long -> long -> Long.

So in the background:

  1. Long.valueOf(lmt) converts Long to long
  2. emp.setLimit(<long>); converts long to Long 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); 
like image 162
Peter Butkovic Avatar answered Oct 06 '22 00:10

Peter Butkovic


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); 
like image 29
jpkrohling Avatar answered Oct 06 '22 00:10

jpkrohling