Comparing doubles in Java gives odd results

前端 未结 5 779
误落风尘
误落风尘 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:33

    just change it to

    System.out.println(d.equals(0d)); // is false ?! now true
    

    You were comparing double with Integer 0

    Under the cover

    System.out.println(d.equals(0)); // is false ?!
    

    0 will be autoboxed to Integer and an instance of Integer will be passed to equals() method of Double class, where it will compare like

    @Override
        public boolean equals(Object object) {
            return (object == this)
                    || (object instanceof Double)
                    && (doubleToLongBits(this.value) == doubleToLongBits(((Double) object).value));
        }
    

    which is going to return false of course.

    Update

    when you do comparison using == it compares values so there is no need to autobox , it directly operates on value. Where equals() accepts Object so if you try to invoke d1.equals(0) , 0 is not Object so it will perform autoboxing and it will pack it to Integer which is an Object.

提交回复
热议问题