I am trying to understand the behavior of the code below which I wrote to experiment on calculation overflow.
public static void main(String[] args) {
System.out.println(getSomeValue());
System.out.println(getFixedSomeValue());
}
private static double getSomeValue() {
return (2500000 - 0) * 250000 * (200 + 310);
}
private static double getFixedSomeValue() {
return (double) (2500000 - 0) * 250000 * (200 + 310);
}
output:
-9.9787264E8
3.1875E14
What I have understood is: It can be because of Integer overflow as:
Double.MAX_VALUE = 1.7976931348623157E308
Integer.MAX_VALUE = 2147483647
I don't understand why the values differ? When the return type of method is double, shouldn't it automatically cast it to double?