0

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"?

Pablo_06
  • 35
  • 5
  • `1/3` is `0`. Because `int`/`int` is an `int`. The fact that you widen that `int` to `float` or a `double` doesn't give an `int` more precision. `(double) (1/3)` is still zero. – Elliott Frisch Apr 12 '20 at 22:53

1 Answers1

2

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110