2

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?

Róbert Garai
  • 53
  • 1
  • 1
  • 7

4 Answers4

5

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);
Blue
  • 545
  • 3
  • 13
Vinod Kumawat
  • 741
  • 5
  • 8
  • aha ... :) 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? – Róbert Garai May 27 '16 at 18:53
  • dear when result will come in integer then it reqiurd to convert in short otherwise precision loss – Vinod Kumawat May 27 '16 at 18:57
  • not sure why you mention unicode in your answer though. – sstan May 27 '16 at 19:00
  • unicode 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 – Vinod Kumawat May 27 '16 at 19:06
2

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.

sstan
  • 35,425
  • 6
  • 48
  • 66
  • thx 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 May 27 '16 at 18:51
  • Normally you would do this by downcasting the `7`, like this: `premennaTypuShort - (short)7`. But afaik, Java doesn't ever perform `short` arithmetic. It will always upcast `short`s to `int` to perform the arithmetic. So you have to be content with the calculation being done with `int` arithmetic, and you then have to explicitly downcast the result to `short`. – sstan May 27 '16 at 18:54
  • ahaa.. 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); – Róbert Garai May 27 '16 at 18:56
  • Well, it can also do `long` arithmetic, so it's not like it can only do `int` arithmetic. But it won't do smaller than `int`, yes. – sstan May 27 '16 at 18:57
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.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

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)
Arindam
  • 24
  • 3