aspose file tools*
The moose likes Beginning Java and the fly likes string concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string concatenation" Watch "string concatenation" New topic
Author

string concatenation

gylph knor
Greenhorn

Joined: Jun 12, 2008
Posts: 20
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
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
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).


Ping & DNS - my free Android networking tools app
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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
Marshal

Joined: Mar 22, 2005
Posts: 42278
    
  64
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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.


Java hobbyist.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string concatenation