In Java, the following code does not compile:int val = 1;short shortVal = val;  // Incompatible typesAnyone know why Java chooses to complain about this assignment, instead of simply truncating, when an integer × integer is also likely to overflow?To illustrate:int val = 46340;int result = val * val;                    // 2147395600System.out.println(result);int overflowVal = 46341;int result2 = overflowVal * overflowVal;   // -2147479015System.out.println(Quietly overflowed:  + result2);
Why disallow int-to-short assignment but allow multiplication and other math?
java;math;data types
In the first case, the compiler knows that you're facing a potential loss of precision, so it can stop you. In the second case the overflow happens during a runtime calculation - though your example trivialy causes an overflow, there is no way for the compiler to check such occurrences in general case so it doesn't.