What does the following Java code print out:
int i = 1234567890;
float f = i;
System.out.println((int)f - i);
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The primitive type
float
is a single-precision floating-point format . According to the IEEE 754 standard, the mantissa holds 24 significant bits. However,1234567890
in binary is1001001100101100000001011010010
, too long (31 bits), and thus must be rounded into a scientific format-like 1 . 0 0 1 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 1 1 0 2 ⋅ 2 3 0 . When cast back to anint
, the above truncation gives1234567936
, and thus(int)f - i = 1234567936 - 1234567890
which is 4 6 .