short s1 = 1; short s2 = 2; short s3 = s1 + s2; // 2
int i1 = 1; int i2 = 2; int i3 = i1 + i2; // 3
Why is it not a compile time error in the 3rd case? Sum of 2 ints can be more than an int value, then why it is giving some -ve value when we try to print the result? [ October 12, 2006: Message edited by: Vamsi Krishna Uppalapati ]
byte b1 = 1; byte b2 = 2; byte b3 = b1 + b2; // Gives error here bcos the result of arithmetic operation + is an int. When the complier performs b1 + b2, the result it gives is of type int. You are trying to assign it a byte which is lesser than an int. You either say byte b3 = (byte)(b1 + b2), or say int b3 = b1 + b2 and that should work for you. I hope you understood this.
The same holds for short.
Regards, Jothi Shankar Kumar. S
SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Vamsi Krishna Uppalapati
posted 13 years ago
Hey Jothi, Thanks alot... I understood now... Thanks again...
binary numeric promotion occurs on these operands:
the multiplicative operators *, / and % (�15.17) The addition and subtraction operators for numeric types + and - (�15.18.2) The numerical comparison operators <, <=, >, and >= (�15.20.1) The numerical equality operators == and != (�15.21.1) The integer bitwise operators &, ^, and | (�15.22.1) In certain cases, the conditional operator ? : (�15.25)
And in binary numeric promotion the following happens:
If any of the operands is of a reference type, unboxing conversion (�5.1.8) is performed. Then: If either operand is of type double, the other is converted to double. Otherwise, if either operand is of type float, the other is converted to float. Otherwise, if either operand is of type long, the other is converted to long. Otherwise, both operands are converted to type int.
So in the above example, the last thing occurs, both operands are converted to an int.
The result of the addition is an int, and you want to store it into a byte, this is a narrowing primitive conversion. It is also an assignment conversion as you store the value of an exprssion (b1 + b2 into a variable. Language specification says in 5.2 Assignment Conversion:
... narrowing primitive conversion may be used if all of the following conditions are satisfied:
1) The expression is a constant expression of type byte, short, char or int. 2) The type of the variable is byte, short, or char. 3) The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.
2) and 3) are ok, but 1): The variable b3 is NOT a constant (is not final), so a compile error occurs.
Why is it not a compile time error in the 3rd case?
The result of the expression i1 + i2; is an int. This int is stored into int-variable i3. So there is no narrowing conversion, and the three cases above are not checked.
all events occur in real time
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad: