0

Getting below bug in findbug:

A boxed value is unboxed and then immediately reboxed Below is the statement:

deliveryCost = new Double(cost);

where cost is float type.

Kindly suggest.

sanbhat
  • 16,864
  • 6
  • 46
  • 62
user2144684
  • 83
  • 11
  • Is it of type `Float` or `float`? –  May 23 '13 at 05:46
  • 3
    Please provide more code. Is the variable `deliveryCost` a `double` or `Double`? – Craig May 23 '13 at 05:46
  • Isn't double and Double the same (just an alias)? – WhileTrueSleep May 23 '13 at 05:48
  • @WhileTrueSleep: **No**. One is an object wrapper, the other is a primitive. There's a pretty sizable difference. – Makoto May 23 '13 at 05:49
  • @Makoto http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string – WhileTrueSleep May 23 '13 at 05:49
  • 2
    @WhileTrueSleep: In C#, `double` is just an alias for `System.Double`, and there is no special name for a boxed `double`. In Java, `double` is the name of the primitive (value) type, while `Double` is the name for a boxed (reference) type for `double`. – Gabe May 23 '13 at 05:52
  • Well string or double doesn't matter but I truely don't know the difference in java - so i guess you're right. Thanks also to other posters :) – WhileTrueSleep May 23 '13 at 05:52
  • Don't use a `float` if you can use a `double` A double has half a trillion times the accuracy and the memory difference isn't worth worrying about 99% of the time. – Peter Lawrey May 23 '13 at 06:23

1 Answers1

3

That message is a little odd. I'm not sure exactly why you're getting it. But one thing you should change is this:

deliveryCost = Double.valueOf(cost);

See if the message goes away when you do that. When you say new Double(...) it's like telling java, "please, allocate a brand new object for me, even if you can reuse one and get the same effect". It's usually not necessary to do.

Daniel Kaplan
  • 54,448
  • 39
  • 189
  • 282