I declared three real number values and tried to compare each value as below codes.
public class Main {
public static void main(String[] args) {
float f = 0.1f;
double d= 0.1;
double d2 = (double)f;
System.out.println(d);
System.out.println(d2);
System.out.println();
System.out.println(d==f);
System.out.println(d==d2);
System.out.println(d2==f);
}
}
In first comparison (d==f
), since the d
is a double type whereas f
is a float type, so the compiler changed the variable f
into double type. It means that the value stored in f
will be reassigned into double type values. Therefore this comparison returns false. And also, d==d2
comparison is executed and returns false as a same way in first comparison.
However, the last comparison d2==f
, it returns true. I expected it returned the false because the variable f
were reassigned into the double type value and the values stored in d2
and f
will be slightly different. However, the results is true.
Can you explain the third comparison why it gave the true?