The first right operand is 4, an integer. This is widened to a double d. Now the result of this expression, the expression (d = 4), is a double.
When you try to assign this value, a double, to an int, it complains.
I believe every expression returns a value which can be utilized- is this true? Certainly every arithmetic expression. This could be investigated- does an assignment of objects return an object?
Oh yes, I guess it is because the right operand is being promoted to double then.
But what's the point then in stating that the right operand is returned, and not the left operand (after assigning it offcourse). Is there any special case where they will not be of the same type?
And yes, assigning object references also returns an object. e.g.
Tom is right The thing here is that compiler doesn't bother about what value is stored in the variable. It just look at the type. And as d is double it sees that you are trying to assigning a double value to interger variable and so yells about the loss of precision...
And this is also a reason that in inheritance some assignments of subclass-superclass are ok at compile time but fail at runtime as compliler is not able to know the value in the variables
Originally posted by Tom Tolman: Could you give an example of failure at run time which works during compile time?
myBase at some point in some far-away part of the program could have been assigned to a Sub object so the compiler has to allow this type of assignment, and if this were the case, the cast would work.