3

I am having difficulty of understanding the behavior of the java in the following scenario. For example, I have a multiply method, which simply multiplies two int value and print the result to the screen.

private static void multiply() {

    int firstValue = Integer.MAX_VALUE;
    int secondValue = 2;                   //double secondValue=2 

    double result = firstValue * secondValue;

    System.out.println("Result is: " + result);
}

and because of the fact that Integer overflow, the result is -2. However, here the calculation result is assigned to a double, which accepts much bigger value than than multiplication of firstValue and secondValue.

My questions to this issue are;

1- Why is Integer overflow happening, although the result is assigned to a double?

2- When I change the type of secondValue to double (mentioned in the comment), the result is correct. Why do Java behave differently when the type of one of the multiplier is changed to double?

2
  • 4
    The calculation is made as an integer and the result is cast to a double only after the calculation. So there's a moment where your int is two times the MAX_VALUE which will yield your result Commented Oct 8, 2016 at 20:33
  • The multiplication only involves integers, so it's done as an integer multiplication. Only once that is done, the result is converted to double, but at that time it's already too late. Cast one of the values in the multiplication. Commented Oct 8, 2016 at 20:34

3 Answers 3

2

Java does not support target type casting.

private static void multiply() {

    int firstValue = Integer.MAX_VALUE;
    int secondValue = 2;
    double one = 1.0;
    double result = one * firstValue * secondValue;

    System.out.println("Result is: " + result);
}

Target Type casting means casting the value of result to the type of the variable it has to be assigned to.
So it does not know that the result has to be assigned to a double variable. In this case, int is the biggest data type, so the expression is calculated in int data type.
if you multiply it with double one, the expression is calculated in double type and the answer is correct.

Sign up to request clarification or add additional context in comments.

2 Comments

However, in your example, when the variable one is at the end (double result = firstValue * secondValue * one) , the integer overflow still happens. I think java looks the order of the variable in the calculation or?
It is because of order of operations.
0

this is expected because when you are multiplying 2 integers the result is an integer only. It works with the otherway when you use double as one of the field the result will be considerd as a double value. the receivers data type doesn't matter here

Comments

0

The result of multiplying two ints is an int, which may or may not overflow, depending on the value of the multiplication. Once this result is produced, it's only then promoted to a double, after the overflow may have occurred.

If one of the operands is a double, the result of the multiplication would be a double, which allows for a much larger range than an int.

3 Comments

However, if you look at the Ankit's example. When I put the variable one, which is double, at the end of the calculation, the integer overflow still happens, even if one of the multiplier is double.
@AdInfinitum That's because order of operations. Two integers are multiplied first overflowing, then it's multiplied by a double and promoted.
@AndrewL. Thank you. You are right. After your comment, I put the last two multipliers (secondValue and one) in a parenthesis and it gives the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.