3
class Test
{
    public static void main(String[] args)
    {
         short s=2,s1=200,s2;
         s2=s+s1; // error: "possible loss of precision"
         System.out.println(s2);
    } 
}

Why does assigning the result of adding two shorts to a short cause a compile error?

Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28
Bhadri
  • 1,599
  • 5
  • 17
  • 14

5 Answers5

5

Because in order to perform arithmetic operations on shorts, the compiler will widen them to integers first:

S2 = s + s1

Is actually

S2 = (int)s +(int)s1

Where the right hand side has type int.

Dirk
  • 30,623
  • 8
  • 82
  • 102
2

Becouse the sum of two shorts will be evaluated as an int, so you are assigning an int to a short.

You can solve this casting it back to short:

s2=(short)(s+s1);
fbafelipe
  • 4,862
  • 2
  • 25
  • 40
1

In this case there may be an overflow, if 32,767 < s + s1 or s+s1 < -32,768

-32,768 - 32,767 is a range of possible values for a short variable.

StKiller
  • 7,631
  • 10
  • 43
  • 56
  • 1
    is it default return type value for arithematic operators in java is int data type??? – Bhadri May 07 '11 at 06:04
  • There is no "default" return type. You simple may exceed the range, because sum of two random numbers from a range can be greater that the range itself. – StKiller May 07 '11 at 06:06
  • @AndreiPodoprîgora If we use `int` instead of `short`, the result does not become a `long` and require a cast; this widening behavior is specific to integer types shorter than `int`. Of course, overflow is still a valid concern, but the warning is simply about the dangers of truncating an `int`, not about the possible overflow. – Theodore Murdock Jun 25 '14 at 23:49
1

Because when you assign a numeric literal, it defaults to Integer. The compiler doesnt inspect the value to check the precision will not be lost.

Additionally, Java will then perform integer arithmetic. See Primitive type 'short' - casting in Java for more information on short values.

Community
  • 1
  • 1
Codemwnci
  • 54,176
  • 10
  • 96
  • 129
  • 1
    It's not in the "short s=2,s1=200,s2;" line that the error occurs, it's in the sum. – fbafelipe May 07 '11 at 06:05
  • 1
    This answer is just wrong. The compiler is smart enough to tell that the literal value won't overflow the short, so the initializations are legal. As others have already noted, Java arithmetic operators widen integer types to int width. Java then requires a cast to return to short width. – Theodore Murdock Jun 25 '14 at 23:42
0

This is due to Java using ints for arithmetic on small integers; after the addition, Java complains about the loss of precision due to casting back to a short.

It's worth noting several things:

  1. The short data type is rarely useful outside of arrays.

  2. If the sum involves the variable you're assigning to, you can avoid the need for an explicit cast by using the += operator, because the compound arithmetic/assignment operators all imply a cast to the type of the original variable (if it would be necessary).

Community
  • 1
  • 1
Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28