-2

I exectued this statement

System.out.println(3.0 + 5/2); 

And found the answer to be 5.0. Now according to promotion, if an expression contains a double type data, every operand will be promoted to double type. So 5 and 2 will be promoted to 5.0 and 2.0 respectively. Therefore the logical expression here should be

3.0+5.0/2.0

Which should give the answer 5.5 instead of 5.0.

  • 1
    In your equation, both `5` and `2` are `int` values. `5 / 2` with `int` division is `2`. That's then coerced to `2.0` and added to `3.0` to produce `5.0`. See the linked question's answers for details. *"if an expression contains a double type data, every operand will be promoted to double type"* True (mostly), but when `5/2` is calculated, there's no double involved (yet). – T.J. Crowder Oct 03 '19 at 14:14
  • 2
    Java follows the standard PEMDAS/BEDMAS order of operations. `5/2` is calculated first, and it does not contain a double type, so integer division is applied. The result of that division is `2`, which is then used in `3.0 + 2.0` (type promotion now applies, because one side of the plus operator is a float) which yields `5.0`. – Jordan Oct 03 '19 at 14:14
  • Not sure you're using the exact JLS terminology here, but you say "if an expression" ... Are you sure `3.0 + 5/2` is an expression ? Is it not rather a `statement` made of multiple `expressions` ? The what matters first is the order of evaluation of the expressions. If you want to know how promotion works, you have to be very specific about the terminology. It's no wonder the java language specification (or any other language for that matter) is soooooooo long. – GPI Oct 03 '19 at 14:23
  • @GPI - `3.0 + 5/2` isn't a statement. It *is* an expression, made up of two expressions (`int` division and `double` addition) or if you want to be really pedantic, five (three of which are literal expressions). Statements and expressions aren't always mutually exclusive, though: `System.out.println(3.0 + 5/2);` is a *statement* made up of a [statement expression](https://docs.oracle.com/javase/specs/jls/se9/html/jls-14.html#jls-StatementExpression) which is a method invocation expression containing the `3.0 + 5/2` expressions. Only some expressions can be used as statement expressions. :-) – T.J. Crowder Oct 03 '19 at 14:47
  • @GPI - If you only have to deal with the JLS, count your blessings. It's got nothing in terms of length and...awkward...prose on the JavaScript spec. ;-) – T.J. Crowder Oct 03 '19 at 14:47
  • 1
    @T.J.Crowder I stand corrected. Thanks. – GPI Oct 04 '19 at 08:25

2 Answers2

1

It's important to remember the order of operations!

3.0 + 5/2
3.0 + (5/2)
3.0 + 2
3.0 + 2.0
5

5/2 executes first, giving back 2.

Hope this helps!

ZektorH
  • 2,680
  • 1
  • 7
  • 20
  • This is an **often** repeated duplicate, it doesn't need Yet Another Answer, just a close vote (when you can) and a comment (provided someone else doesn't get there first). – T.J. Crowder Oct 03 '19 at 14:12
0

By order of operations, division is done before addition. 5/2 are both integers, so the result is an integer 2. 3.0+2 = 5.0 (the two is "promoted" but doesn't affect the result)

The type promotion doesn't happen until the operarion involving a double and only affects the involved parameters, not the whole equation

Ali
  • 406
  • 5
  • 15
  • This is an **often** repeated duplicate, it doesn't need Yet Another Answer, just a close vote (when you can) and a comment (provided someone else doesn't get there first). – T.J. Crowder Oct 03 '19 at 14:12