JAVA cannot convert from int to short
Solution 1
java is 32 bit. That means that whenever any arithmetic operation is performed it will return in 32 bit value. So, you must cast it to short again, like so:
int a = 10;
short premennaTypuShort = (short)a;
premennaTypuShort =(short)(premennaTypuShort - 7);
Solution 2
When you're using a byte
, a short
or a char
to perform arithmetic operations involving ints
, there is an automatic promotion to the int
primitive type.
Here, you're trying to assign an int
back to a short
.
A solution would be the assignment operator -=
, this will avoid the conversion to an int
int a = 10;
short premennaTypuShort = (short)a;
premennaTypuShort-=7;
System.out.println(premennaTypuShort); // 3
WARNING
The assignment operator has a bad side too. Look at the following code.
short s = Short.MAX_VALUE;
s+=1;
System.out.println(s); // -32768
By adding 1
to Short.MAX_VALUE (32767
), you're overflowing the short and will get unexpected results.
Solution 3
The problem is that in order to calculate premennaTypuShort - 7
, premennaTypuShort
first needs to be converted to an int
, so the result of the calculation is an int
.
This, in turn, means that you are then trying to assign an int
back to a short
variable, which requires an explicit downcast on your part.
Solution 4
Additive operators (+ and -) are "Numerical Integer Operator". And Numerical Integer Operators always produce a value of type int or long.
Because, any integer operator except shift operator that has at least 1 operand which is of type long is carried out using 64 bit precision and the result would be of type long.
Otherwise the operation is carried out using 32 bit precision and the result would be of type int.
That is why here the expression premennaTypuShort – 7
is producing a result of type int
and to store an int
value to a short
you need to specifically cast it to short
like following, which is known as narrowing.
premennaTypuShort = (short)(premennaTypuShort - 7)
Related videos on Youtube
Róbert Garai
Updated on June 14, 2022Comments
-
Róbert Garai almost 2 years
Hi I am new one here and this is my first question: I have a code with simple aritmetic operator. But it won't works:
int a = 10; short premennaTypuShort = (short)a; /*retyped variable a into short type and entered into new variable*/ premennaTypuShort = premennaTypuShort - 7; /*now i am trying to decrease the number but eclipse wrote that cannot convert from int to short.*/
i am trying to decrease the number specified as short but eclipse wrote that cannot convert from int to short. I don't undertand why. so where is the problem ? how can I repair this error?
-
Deepanshu almost 8 yearsWhenever dealing with short and int arithmetic operations java return result in
int
so you have to typecast that intoint
-
-
Róbert Garai almost 8 yearsthx for quick reply. -yes I am trying to do this aritmetical in short type. not integer. because I want the result in short type as well. how to do this explicit downcast?
-
Róbert Garai almost 8 yearsaha ... :) that second part premennaTypuShort =(short)(premennaTypuShort - 7); I didn't know that another (short) is necesssary... why another? it was defined as short in first line. isn't it?
-
sstan almost 8 yearsNormally you would do this by downcasting the
7
, like this:premennaTypuShort - (short)7
. But afaik, Java doesn't ever performshort
arithmetic. It will always upcastshort
s toint
to perform the arithmetic. So you have to be content with the calculation being done withint
arithmetic, and you then have to explicitly downcast the result toshort
. -
Róbert Garai almost 8 yearsahaa.. i understand now .. any kind of aritmetic is done as integer and therefore if i want the result into short i need to type this premennaTypuShort =(short)(premennaTypuShort - 7);
-
Vinod Kumawat almost 8 yearsdear when result will come in integer then it reqiurd to convert in short otherwise precision loss
-
sstan almost 8 yearsWell, it can also do
long
arithmetic, so it's not like it can only doint
arithmetic. But it won't do smaller thanint
, yes. -
sstan almost 8 yearsnot sure why you mention unicode in your answer though.
-
Vinod Kumawat almost 8 yearsunicode means java work on all available character in world by define unicode so that in java integer is 4 bit but in C,C++ 2 bit