0

I came across this question, and I can vaguely tell it seems to be handling the number part as: A + B * C --> A * B + B * C

similarly, there are 3 more examples

1)

jshell> System.out.println("Sum is: " + 3 + 10*0.008);
Sum is: 30.08
jshell> System.out.println("Sum is: " + 33 + 10*0.008);
Sum is: 330.08
jshell> System.out.println("Sum is: " + 33 + 1000*0.8);
Sum is: 33800.0

According to String Concatenation, shouldn't it be:

  1. is "Sum is: 3.8"
  2. is "Sum is: 33.08"
  3. is "Sum is: 33800"

but it's not.

I remember we can even easily turn numbers into String by simply doing "" + number; and if it was "" + number + number --> would be ""numbernumber, not ""2number

How are these results produced exactly?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Oyghan
  • 11
  • 1
  • 1
    Wrap the mathematical part inside brackets so that it behaves like a mathematical equation rather than string concatenation `System.out.println("Sum is: " + (3 + 10 * 0.008));` and the sum will be `3.08` – sorifiend Aug 30 '22 at 04:57
  • 1
    Hint: what does it look like if you write a `3`, and then multiply `10 * 0.008` and put the result right next to the `3`? (Hint: if you compute `10 * 0.008` **and display it as a string**, there will be a `0` in front of the `.`, right?) – Karl Knechtel Aug 30 '22 at 05:00
  • Welcome to Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**; we are looking for *specific, answerable questions*, and "can someone shed some light into this please" [does not qualify](https://meta.stackoverflow.com/questions/284236). We can't tell you why it doesn't do what you expect, because *we have no way to know why you expect what you expect*. – Karl Knechtel Aug 30 '22 at 05:02
  • @sorifiend : Thank you for the tip! if I want to get the precise result that is the right way to print out the value. appreciate your response! – Oyghan Aug 31 '22 at 05:14
  • @KarlKnechtel: This kinda what I was looking for, this was some of the basic concatenation, but my brain for some reason didn't even try to write it out, or even try to calculate each operation separately and then look at it. You had pointed out the mental laziness I performed. Thank you Karl!! – Oyghan Aug 31 '22 at 05:15

1 Answers1

2

This is a problem of associativity. + is left associative. a + b + c is ((a + b) + c).

Similarly, a + b + c * d is

((a + b) + (c * d))

not

(a + (b + (c * d)))

You can treat (c * d) as "one term" here because * has higher precedence than +.

Normally this doesn't make much difference if a, b, c are numbers, but in your case

("Sum is: " + 3) + (10 * 0.008)

The first + operates on a String and int, so it does string concatenation, which makes the first parenthesised expression a string. This makes the second + also do string concatenation.

On the other hand, if it were

("Sum is: " + (3 + (10 * 0.008)))

The second + operates on an int and a double, so it does numeric addition.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you @Sweeper!! if I could I would have upvoted it, since I am new, wan't be able to give your the appreciation you deserved. Thank you for your detailed answer!! – Oyghan Aug 31 '22 at 05:17