2

This might seem simple but I don't have any answer. When I write:

 System.out.println (100 * 1000 * 10000 * 100000);
 System.out.println (100 * 1000 * 10000 * 100000.0);

it returns these values:

276447232
1.0E14

I understand that this has something to do with maximum values of some data types. But I would just like a clear answer as to why it returns these exact values for both equations. If someone can explain this to me I will be very appreciative.

The first return doesn't match the maximum value for int datatype, that's why I'm confused. And the second return I'm assuming is a double or float value, but I'm not sure.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
mackanmorre
  • 145
  • 1
  • 13
  • "The first return doesn't match the maximum value for int datatype, thats why I'm confused" - well it's already overflowed. It doesn't reach the maximum and stay there. – Jon Skeet Apr 26 '17 at 20:46
  • 1
    `100000000000000 % (2^32)` is 276447232... – Jon Skeet Apr 26 '17 at 20:47
  • (And yes, the second value is a double value - it's not clear where you're confused there.) – Jon Skeet Apr 26 '17 at 20:48
  • Possible duplicate of [What is the maximum number in the mantissa part of a Java float?](http://stackoverflow.com/questions/5849741/what-is-the-maximum-number-in-the-mantissa-part-of-a-java-float) – Timothy Truckle Apr 26 '17 at 20:51
  • @JonSkeet ok thanks. Just to be clear, the second returned value is supposed to be like that and is not the wrong value right? – mackanmorre Apr 26 '17 at 20:53
  • Yeah it's obviously integer overflow, I'm sure that has been answered here already. – markspace Apr 26 '17 at 20:54
  • This question seems like it might answer the question: http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo – markspace Apr 26 '17 at 20:55
  • Yes, that's 1x10^14. – Jon Skeet Apr 26 '17 at 20:55
  • @mackanmorre `int * double` will always give you `double`. And `1000000000 * 100000.0` is certainly `100000000000000.0`. Don't be thrown by the scientific notation with which that last number is shown. – Dawood ibn Kareem Apr 26 '17 at 20:56
  • Ok thanks all of you! The question has been answered :) – mackanmorre Apr 26 '17 at 20:57

1 Answers1

6

In the expression

System.out.println (100 * 1000 * 10000 * 100000);

The parameter is an int and the result exceeds the maximum value admissible for an int which is 2147483647. This is what we call an overflow. According to the Java Language Specification

If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format.

Taking the N low-order bits of a number is equivalent to computing the remainder of the division of this number by 2^N. In our case, N=32 because int are stored on 32 bits. This why Jon Skeet answered by saying

100000000000000 % (2^32) is 276447232.

In the expression

System.out.println (100 * 1000 * 10000 * 100000.0);

The product of the three first factor 100 * 1000 * 10000 gives 1_000_000_000 which is less than the maximum int value. The last multiplication leads to a Binary Numeric Promotion which means, in this case, that 1_000_000_000 is converted (promoted) to double and then multiplied by 100000.0.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240