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?

4
  • 1
    Whenever dealing with short and int arithmetic operations java return result in int so you have to typecast that into int Commented May 27, 2016 at 18:52
  • premennaTypuShort -= 7; Commented May 27, 2016 at 19:02
  • typecast that into short not in int sorry for typo Commented May 27, 2016 at 19:05
  • stackoverflow.com/questions/2294934/… Commented May 27, 2016 at 19:05

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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?
dear when result will come in integer then it reqiurd to convert in short otherwise precision loss
not sure why you mention unicode in your answer though.
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
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.

4 Comments

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?
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 shorts 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.
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);
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.
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.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.