0

Why [double divide_ = 9 / 2;] gives output of 4.0? while [double divide = (double) 9 / 2] gives an output of 4.5?

  • 3
    the division is NOT using `double`, just the result is being assigned to the `double` || specified in [JLS 15.17. Multiplicative Operators](https://docs.oracle.com/javase/specs/jls/se19/html/jls-15.html#jls-15.17-320): "* If the promoted type is int or long, then integer arithmetic is performed.*" – user16320675 Oct 16 '22 at 14:08
  • Use the float type – Mansour Faouziel Oct 16 '22 at 14:09
  • BTW code is formatted by using single back-ticks ` around it or, for multi-line code, using 3 (or more) back-ticks on a line before and after the code (or indenting the code by at least 4 spaces) – user16320675 Oct 16 '22 at 14:16

0 Answers0