• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

string concatenation

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic