Shivraj
Shivraj

Reputation: 31

For the line of code below I am getting "Boxed value is unboxed and then immediately reboxed" findbug.. How can this be resolved?

Double a = new BigDecimal(Double.valueOf(x))
    .setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue();

x is of type Double, which I cannot change..

Upvotes: 1

Views: 647

Answers (2)

user2357112
user2357112

Reputation: 281446

Double.valueOf takes a primitive double and returns a boxed Double, not the other way around. You're passing it a boxed Double, so x is autounboxed, then explicitly boxed by Double.valueOf, then autounboxed again to pass it to new BigDecimal. Just remove the valueOf call:

Double a = new BigDecimal(x).setScale(2,BigDecimal.ROUND_HALF_DOWN).doubleValue();

This will autounbox x once to pass it to the BigDecimal constructor.

Upvotes: 2

barfuin
barfuin

Reputation: 17494

If x is of type Double, then Double.valueOf(x) is the cause of the warning. Just write x instead, which is already of the correct type.

Upvotes: 2

Related Questions