Why disallow int-to-short assignment but allow multiplication and other math?
https://softwareengineering.stackexchange.com/questions/126167
-
03-11-2019 - |
Question
In Java, the following code does not compile:
int val = 1;
short shortVal = val; // Incompatible types
Anyone 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; // 2147395600
System.out.println(result);
int overflowVal = 46341;
int result2 = overflowVal * overflowVal; // -2147479015
System.out.println("Quietly overflowed: " + result2);
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with softwareengineering.stackexchange