Originally posted by Ravinder Singh Ghai: Dear friends,
I have following queries related to the above-mentioned subject matter:
1) Expressions: short h = 40; // OK: int converted to short h = h + 2 // Error: cannot assign an int to short
Q: Why does the second expression flags error when the operands on the right side totals to 42 (after one operand is promoted to int) which is pretty much in the range of short variable h?
2) byte b = 32; float f = 3.5F; double v = (f * (- b));
Q: What will be the result of this expression and most importantly how is unary - b handled?
Awaiting responses. Thanks in advance!
Whenever two whole numbers are used in a binary operation like +, if either operand is a long, the the other operand is promoted to a long. Otherwise all operands are promoted to int or remain int. You can assign an int to a short without an explicit cast.