You are facing a problem as your number wraps around . In the first case, it does not wrap and, therefore, overflows the range shorter. But in the second case, it wraps after the calculation and, therefore, it falls into the short range and therefore you do not have a compile-time error.
Loss of accuracy means that you are losing information about a given value (the short data type is a 16-bit signed integer of two additions, the minimum value is 32.768 and the maximum value is 32,767 (inclusive).) In your first case, the range of short ones intersects (1073741824 ), and therefore you are losing information.
Narrowing the conversion of a signed integer to an integral type T simply discards everything except n bits of the lower order, where n is the number of bits used to represent type T.
EDIT: -
From JLS ยง3.10.1 (very correctly indicated in this similar question)
This is a compile-time error if the decimal literal of type int is greater than 2147483648 (2 31 ), or if the decimal literal 2147483648 appears anywhere other than the operand of the unary minus operator ( ยง15.15.4 ).
Rahul Tripathi Jul 16 '14 at 5:46 a.m. 2014-07-16 05:46
source share