Learn something every day
I had a rule in my head for implicit type conversions of primitives: if one operand is "wider" than the other, "promote" the smaller one. For example: given
WRONG! Well, partly. The example above is correct, but consider this:
There's rules for binary operators and rules for unary operators.
int narrow = 1; long wide = Long.MAX_VALUE;then the expression
wide - narrow is equivalent to wide - (long)narrow and the result is long.WRONG! Well, partly. The example above is correct, but consider this:
byte narrow = (byte)1; short wide = Short.MAX_VALUE;The expression
wide - narrow is equivalent to (int)wide - (int)narrow and the result is int. But I didn't discover that until I'd had a lot of fun with "possible loss of precision" errors from the compiler.There's rules for binary operators and rules for unary operators.
