-2

I think 5 is an int. But there are no variables. I don't know why the output is a double. I tried in Netbeans already.

Yixuan Lu
  • 1
  • 1

1 Answers1

0

Multiplication and division are done left to right (and regardless, you put parentheses around the first operation), and if you divide any primitive umber by a double, you will get a double out of it.

5/2.0 will clearly be evaluated first.

So, even though ‘5’ is an int, if you do 5/2.0, you’ll get a double (namely, 2.5). Now, at this point, your code is already working with a double.

When you multiply 2.5 by 2, you then output 5.0, since 2.5 is a double.

Sal
  • 302
  • 1
  • 11