0

I have the following code

Map<Integer, Double> map = new HashMap<Integer, Double>();
Double number = map.get(0); //suppose this equals 1
double number2 = objectThatReturnsPrimitiveDouble.getQuantity();//suppose this equals 2
System.out.print(number < number2); //spits out true

I am wondering if comparing the values of number and number2 is good practice even though one is a primitive type and the other is a Class type. Or will it be acceptable to change the primitive type of number2 to Double?

ali
  • 15
  • 5
  • I don't think `Double < Double` will work... – MC Emperor Sep 19 '19 at 19:47
  • @jrook Note that the code here is comparing with `<`, not `==` as in the question you linked. Also, that question doesn't address the difference between `Double` and `double` which is the more important issue here. – Code-Apprentice Sep 19 '19 at 19:48
  • 1
    Beware of NPEs. `map.get(0)` could return `null`. – MC Emperor Sep 19 '19 at 19:49
  • @MCEmperor yep exactly. The reason why I asked this question was that map.get(0) did return null and i had a null pointer exception which made me think what will be the best approach. – ali Sep 19 '19 at 19:55
  • @DwB not sure how this is a duplicate to the question you have linked. – ali Sep 19 '19 at 20:01
  • The linked question is indeed not a duplicate of this question at all. – MC Emperor Sep 19 '19 at 20:01
  • @ali If you either check if `map.get(0)` is `null` or guarantee that it is never `null`, then comparing a `Double` to a `double` is perfectly fine, due to auto-unboxing. Note that the wrapper type (`Double`) is always unboxed, and not the other way around. This results in both the left-hand expression as the right-hand expression being a `double`, which in turn makes the operator `<` valid there. – MC Emperor Sep 19 '19 at 20:04
  • I still think this is a duplicate. I don't think a question should have exact same wording to be marked duplicate. Here, the OP is puzzled why they are getting wrong result out of a double comparison to which the answer has been given in the linked question. This is the first step to take. Unboxing is not the real issue here and just telling the OP that unboxing is fine will not really help them. – jrook Sep 19 '19 at 20:13
  • Duplicate or not, I strongly suggest that @ali reads https://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java as in my opinion, there is a strong possibility that this is the root of the problem in this case and the first thing to make sure is "fixed". – jrook Sep 19 '19 at 23:29

3 Answers3

2

Yes, this is good practice. Java makes auto-unboxing Double -> double.

P.S. By the way, correct comparison of double is Double.compare(one, two) == 0

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

Yes, comparing a Double to a double in this way is just fine. This is because Java automatically converts the Double to a double anyway through auto-unboxing.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

In your code, the variable number (Double) will be autounboxed to primitive double. The comparison will work.

You can read about autoboxing and unboxing here: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html