62

In Java when we declare

short number=1024*1024*1024; 

it will give compile time error but

short number=1024 * 1024 * 1024 * 1024;

compiles fine. Why does this happen?

Andrew Tobilko
  • 44,067
  • 12
  • 74
  • 128
Jekin Kalariya
  • 3,255
  • 2
  • 18
  • 31
  • @Raedwald,Erwin,Ivan,Carlos My question is different than the question you mentioned.here the question is not only about literal and run time evaluation of number , but its relates to range of short , in above question even though none of expression is literal, first one gives error, question is that why such behavior happen in compilation? any ways thanks, now I satisfied with below answers. – Jekin Kalariya Jul 23 '14 at 11:43
  • good question +1 from my side – Bhargav Modi Feb 28 '15 at 11:05

2 Answers2

72

The compiler will, in this case, evaluate the calculation (because it contains only constants) and try to assign the result to the variable. This calculation is done with type int and only converted to short on assignment, if at all possible.

In your case, the first calculation is too large to fit into a short (1073741824). The second one will overflow the int and end up in a range that short supports (0). So the assignment works in that case.

Mind you, you probably don't ever want to rely on these things in code.

Andrew Tobilko
  • 44,067
  • 12
  • 74
  • 128
Joey
  • 316,376
  • 76
  • 642
  • 652
  • 10
    We can see this as well by changing one of them to `1024L`, in which case the calculation is done as a `long` and overflow doesn't occur, causing a compile error again. – Chris Hayes Jul 16 '14 at 05:44
  • 1
    @user2169777:- A loss of precision means that you are losing information of the given value.(*The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).*) In your first case the range of short is crossed(1073741824) and hence you are loosing the information. So you are receiving the error. – Rahul Tripathi Jul 16 '14 at 05:56
11

You are facing the problem as your number is wrapping around.In the first case it does not wrap around and hence it overflows the range of short. But in the second case it wraps around after the calculationa and hence it comes in the range of short and so you dont have the compile time error.

A loss of precision means that you are losing information of the given value.(The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).) In your first case the range of short is crossed(1073741824) and hence you are loosing the information.

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.

EDIT:-

From JLS §3.10.1(very correctly mentioned in this similar question)

It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).

Community
  • 1
  • 1
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299