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();