I was going through:
Why is a round-trip conversion via a string not safe for a double?
and just wanted to see what happens such situations in Java.
public class RounfTripConversion {
public static void main(String[] args) {
Double d1 = 0.84551240822557006;
String s = d1.toString(); // String("r");
Double d2 = Double.parseDouble(s);
System.out.println("Double 1:" + d1);
System.out.println("Double 2:" + d2);
boolean equal = d1 == d2;
System.out.println("Double 1 and 2 are EQUAL is " + equal);
System.out.println("Double 1 - Double 2=" + (d1 - d2));
}
}
Output:
Double 1:0.8455124082255701 Double 2:0.8455124082255701 Double 1 and 2 are EQUAL is false Double 1 - Double 2=0.0
Can someone help me understand why a boolean comparison that returns both d1
and d2
are not identical, although that's the case?