The key to understanding why there is no difference is to understand what this does:
double d = 10 / 3.0f;
First the 10
is promoted to 10.0f
.
Next the expression 10.0f / 3.0f
is evaluated to give a float
value.
Finally, float
value is promoted to a double and then assigned to d
.
In short, 10 / 3.0f
is using float
arithmetic and producing a float
value. And indeed, the same thing happens here:
float f = 10 / 3.0f;
except that we are skipping the promotion to a double
.
Now to this:
System.out.println(d == f);
What happens here is that f
is promoted to a double
, and that value is then compared with d
.
And ... they are the same.
Because the value of d
is also produced by the same sequence of operations; see above. (It is just that the float
to double
promotion happens at a different point in the code.)
By the same reasoning ... in d - f
, the f
will be promoted before it is subtracted, and you are going to be subtracting a value from itself.
Note: the technically correct Java term for "promotion" I am talking about is a "primitive widening conversion". But that is a bit of a mouthful.