Comparing doubles in Java gives odd results

前端 未结 5 834
误落风尘
误落风尘 2021-02-05 16:54

I really can\'get my head around why the following happens:

Double d = 0.0;
System.out.println(d == 0); // is true
System.out.println(d.equals(0)); // is false ?         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 17:31

    When you perform

    d == 0
    

    this is upcast to

    d == 0.0
    

    however there are no upcasting rules for autoboxing and even if there were equals(Object) gives no hits that you want a Double instead of an Integer.

提交回复
热议问题