This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Class is not Immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Class is not Immutable" Watch "String Class is not Immutable" New topic
Author

String Class is not Immutable

Ram Pathan
Greenhorn

Joined: May 18, 2002
Posts: 6
How can you say that strings are immutable
when you can do an operation like
String motto="Program Once"
motto=motto+"execute everywhere"
motto=motto.concat("dont bet oin it!");
(Plz refer Mughal and rasmussen page 311)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And everyone of those creates a completely new and distinct object. You haven't created one String object... you have created three!


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Rama Krishna Pathangi:
How can you say that strings are immutable
when you can do an operation like
String motto="Program Once"
motto=motto+"execute everywhere"
motto=motto.concat("dont bet oin it!");
(Plz refer Mughal and rasmussen page 311)

Because none of these operations modify a String object. Rather, each and every one of these creates a brand new String object. Take this example:

This code snippet generates the following output:

If the original String object had actually been modified, both variables would have referenced the same String. Rather, when the concat method was invoked, a new String was created and returned from that method.
The String object is, indeed, immutable.
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The methods on String objects that seems to modify them, don't do that. They just create a new String and return it. The original string object is untouched. If you assign the returned string object to the same variable, that variable just point another object. The original string didn't change.
It is the same for the + operator.
Take a look at this code:


SCJP2. Please Indent your code using UBB Code
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Thomas Paul:
And everyone of those creates a completely new and distinct object. You haven't created one String object... you have created three!

Hence the reason why String Concatenation is such an expensive operation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Class is not Immutable
 
Similar Threads
Confusion about string objects
Immutable
Strings Immutable ??
HashMap keys
hi all - cleared SJCP with 86 %