0

I am executing this single Java line:

System.out.print(5/9*(0-32));

The result I am expecting is:

-17.77

But it is instead printing 0.

It works if I do the following casting:

System.out.print((double)5/9*(0-32));

My question is: why do I need to do this casting? Moreover, shouldn't it print 17 if it is translating that expression to an int?

Mr Guliarte
  • 699
  • 1
  • 10
  • 24
  • Without the cast to double, all the elements in your expression are integers, so the division is done according to the rules of integer division. – NomadMaker Apr 18 '21 at 18:49
  • Just to make it clear: `5/9*(0-32)` does not result in a floating point number. As @NomadMaker says, it results in an `int`. On the other hand `(double)5` is a floating point number (same as `5.0`), so `(double)5/9*(0-32)` does result in a floating point number, a `double`. – Ole V.V. Apr 18 '21 at 19:21

0 Answers0