Darko Jakimovski wrote:. . . += and s = s+ should do the same job.
Why should they? The Java® Language Specification doesn't say they behave the same; in fact it says that += and similar operators behave as if they incorporated a cast, which is different from +. It also says that String concatenation behaves differently from addition. Also the addition operator is an commutatve operator, but the String concatenation operator isn't.
When you concatenate Strings you go from left to right, as you do for all expressions in Java®. So you do the process in the orderWhereas in the second example you are adding 1 2 3 to make 6, before you concatenate it onto thee String. You can get the same result with...because you are now doing the addition first. The + operator associates to the left, but in the last code block you are executing the middle and right +s before that on the left.
posted 4 weeks ago
Makes sense now. Because when you use += Java first looks at what's there to add to the variable, evaluates it and returns a new String to the variable. In this case there is an arithmetic operation to be done first. Or did I get it wrong?
Thanks for the reply Campbell, I'll be posting similar questions in the upcoming week, I'm getting ready for the OCA and I'm writing anything I can possibly think of and compiling it so I can be the compiler.
Yeah, that's what got me mixed up. I know when you add any type of String everything after that is treated as String that's why += confused me so much, because everywhere += means identifier = identifier +; So... Yeah... All is good now Thank you for the reply anyhow. Awesome possum