This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Concatenation of Strings using +=

 
Ranch Hand
Posts: 33
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I thought I knew x+=3 is the same as x = x +3. Turns out not quite. So this is what I did messing around and found myself baffled.
Some code ought to do the work of explaining.



outputs " 3123"

While



Outputs " 36"

The question is why? Since += and s = s+ should do the same job.



 
Marshal
Posts: 65050
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Darko Jakimovski
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
 
Saloon Keeper
Posts: 10415
223
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is just in operator precedence. + has much much higher precedence than +=, so if we make it explicit by adding parentheses, we get this:

As you can see, in the first snippet, s+1 finishes evaluating first, while in the second snippet 1+2 finishes evaluating first.
 
Darko Jakimovski
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG thank you so much Stephan. Much clearer now.
 
Bartender
Posts: 2326
100
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also try this one ...
 
Darko Jakimovski
Ranch Hand
Posts: 33
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@salvin francis

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!