Primitive type 'short' - casting in Java

Posted by gemm on Stack Overflow See other posts from Stack Overflow or by gemm
Published on 2009-01-25T14:30:48Z Indexed on 2010/05/02 2:47 UTC
Read the original article Hit count: 513

Filed under:
|
|
|

Hello,

I have a question about the primitive type 'short' in Java. I am using JDK 1.6.

If I have the following:

short a = 2;
short b = 3;
short c = a + b;

the compiler does not want to compile - it says that it "cannot convert from int to short" and suggests that I make a cast to short, so this:

short c = (short) (a + b);

really works. But my question is why do I need to cast? The values of a and b are in the range of short - the range of short values is {-32,768, 32767}. I also need to cast when I want to perform the operations -, *, / (I haven't checked for others).

If I do the same for primitive type int, I do not need to cast aa+bb to int. The following works fine:

int aa = 2;
int bb = 3;
int cc = aa +bb;

I discovered this while designing a class where I needed to add two variables of type short, and the compiler wanted me to make a cast. If I do this with two variables of type int, I don't need to cast.

Thank you very much in advance.

A small remark: the same thing also happens with the primitive type byte. So, this workes:

byte a = 2; byte b = 3; byte c = (byte) (a + b);

but this not:

byte a = 2; byte b = 3; byte c = a + b;

For long, float, double, and int, there is no need to cast. Only for short and byte values.

© Stack Overflow or respective owner

Related posts about short

Related posts about cast