Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string concatenation

 
gylph knor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a piece of code that complies and runs fine:



how is this happening?
we know that string objects are created once and have the same value through their lifetime!!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are indeed immutable, but "s" is not a String - it is a reference to a String object. As such, it can point to different objects over time. If you want "s" to be immutable, declare it "final" (which you can't do in this case because s is a local variable, but which would be possible if s was an instance variable).
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add on top of what Ulf has said and if you were intended to ask, how come the value being pointed by S is changing -- as Ulf told, "s" is not an object here but a reference to a string object.

Once you assign the modified/updated content to the same reference variable "s", the original value is not actually getting modified. Instead a brand new string object is assigned to the same reference variable "s".

In your example itself,



At line 2, the concatenated String "Fred47" is being constructed newly and that is what getting assigned to "s". The original strings "Fred" and "47" are not modified and left as it is.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
If you want "s" to be immutable, declare it "final" (which you can't do in this case because s is a local variable, but which would be possible if s was an instance variable).


Local variables can be "final", can't they? We don't do it often in Java, but it's legal, I think.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes local variables can be final, and parameters can be final too.

I have just finsihed editing some code that used a final local variable.
[ July 01, 2008: Message edited by: Gavin Tranter ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
Local variables can be "final", can't they?

They sure can. I had (for unrelated reasons) visibility modifiers on my mind when I wrote that. Sorry for the confusion.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

We don't do it often in Java, but it's legal, I think.


Well, *we* actually do it often. In fact our team has activated the eclipse setting that automatically declares local variables as final whenever possible (as a save action). It's quite helpful.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree on the use of this keyword. If a variable or parameter is not meant to be changed, declare it final. Any issue raised by the compiler due to this is a signal worth looking into, because the code is obviously doing something you didn't expect or something you forgot you intended.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic