0

How does string concatenation work here? As the return value is of type String here, so everything should be converted to string. But why is it printing "30Good3040morning" here, instead of "1020Good3040morning". Please Help.

class StringConcatinationWorking{
    public static void main(String ...args){
        String s1 = 10 + 20 + "Good" + 30 + 40 + "morning";
        System.out.println(s1);
    }
}
Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29
tramboo
  • 25
  • 6
  • 8
    without brackets the expression is evaluated according to normal precedence rules, in this case simply from left to right. So it will first evaluate the 10 + 20 part which equals 30, then add the "Good" part, resulting in a String type. As of this point everything else will be added as alphanumeric parts. – TheWhiteRabbit May 20 '19 at 13:17
  • 1
    If you really need to do a concatenation like this, I would recommend using `StringBuilder`. The performance would only possibly get better (at worse staying the same), and it would avoid the implicit conversion problem you are having now. – Tim Biegeleisen May 20 '19 at 13:18
  • **10 isn't a String**, it's an `int`, and it's converted to string when concatenate to other String – Ori Marko May 20 '19 at 13:23
  • because before 30 there is a string so, string concatenation("Good"+30)=Good30 and after that "Good30"+40 will be "Good3040" and goes on – Madhusudhan Aradya May 20 '19 at 13:24

2 Answers2

6

Remember that the + operator is left associative, so it "puts brackets" from left to right. String concatenation is only performed when at least one of the operands is a String.

Note that things like 10 and 30 are not Strings. They are int literals.

Your expression, after putting brackets, becomes:

(((((10 + 20) + "Good") + 30) + 40) + "morning")

If we evaluate step by step, starting from the innermost bracket, we get:

((((30 + "Good") + 30) + 40) + "morning") // 10 + 20
((("30Good" + 30) + 40) + "morning") // 30 + "Good"
(("30Good30" + 40) + "morning") // "30Good" + 30
("30Good3040" + "morning") // "30Good30" + 40
"30Good3040morning" // "30Good3040" + "morning"

Notice how we get a subexpression of 10 + 20, but not a subexpression of 30 + 40.

To get your expected result, just add a "" term before or after the 10 term, so that the brackets become:

((((((10 + "") + 20) + "Good") + 30) + 40) + "morning")
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Rules of addition apply: Evaluate left to right, multiplication and division first.

10 + 20 + "Good" + 30 + 40 + "morning"

First 10 + 20 is seen, integer + integer. No String seen. Okay, make integer 30.
then a String is seen, integer + String. Change type to String "30" + "Good" = "30Good"
then all is seen with one String at least and converted to String.

To have everything as String, use a StringBuilder and put the values into that to get to a String.
Or add a "" in front of the concatenation list, to start out with a string to turn everything to String, with exception of possible multiplications or subtractions.

"" + 10 + 20 + "Good" + 30 + 40 + "morning"

Same rules of addition apply if you have a multiplication or division in there. Those precede addition or subtraction

 10 + 20 + "Good" + 30 * 40 + "morning" == "30Good1200morning"

 10 + 20 + "Good" + 30 / 40 + "morning" == "30Good0morning"

In cases like these I like to use a StringBuilder, that way you have fine grained control on what get's appended and you can just forget about the order of addition and multiplication rules that might apply by these mixed types, and the code becomes more readable and self documenting. see it online

String newstr = new StringBuilder()
                    .append(10)
                    .append(20)
                    .append("Good")
                    .append(30)
                    .append(40)
                    .append("morning")
                    .toString();
Tschallacka
  • 27,901
  • 14
  • 88
  • 133