and the implicit downcasting at assignments will be done only for literals in range ( 7 is a int literal in range of short primitive type) -( this is our case) and for the constants.
May I also add that if the value is not within the range of the short (-32,768 to 32,767 (inclusive)) an explicit cast is required and that this value must be within the range of the int (-2,147,483,648 to 2,147,483,647 (inclusive)). Note that all literal values are integers UNLESS they are denoted as floats or doubles. See the following code snippet:
It is true that the + operator returns integers even if the arguments are shorts and that you must cast the result to a short. However this only applies when the operation includes a variable. Like this:
However in the following two circumstances it is not required to cast to short:
Applies equally for all arithmetic operators (+, -, *, %, /) and byte, char and integers, but not floats or doubles.
I have a question about implicit downcasting from long to int.
int a = 9 L //won't compile and implicit downcasting won't work
int a = (int) 9 L //compile
We need to do explicit downcasting.
But short a = 9 // implicity downcasting works.
Why implicit downcasting works for short, not for long?
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Gift giving made easy with the permaculture playing cards