Double happens:
double ages[] = new double[] {2D, 3D, 4D, 5D};
ages[0] = 7/2;
Index Element
0 3.0
1 3.0
2 4.0
3 5.0
Makes sense because:
7 int / 2 int = 3 int
But when it gets appended at the final the suffix D or F to ages[0] = 7/2;
, INDEX 0 appears with decimals even when we are dividing integers:
double ages[] = new double[] {2D, 3D, 4D, 5D};
ages[0] = 7/2D;
Index Element
0 3.5
1 3.0
2 4.0
3 5.0
Why its getting truncated even if the suffix D suppose to be "OPTIONAL"?
7 / 2
is the int/int
operation and therefore the result is 3
. When you assign it to a float
or double
variable, it will become 3.0
.