string vs string builder used in java

Hi,


I would like to know string vs string builder. When to use each one. How they are different and similar. How they differ in terms of immutability. I am still not very clear on creating, assigning , reassigning the instance variables of them and how they point to same or different objects etc.

please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Strings are immutable.
StringBuilders are mutable.

That's the main difference and from that everything else important follows.

If you have a string that you are modifying then you should use a StringBuilder - since it supports modification explicitly (since it's mutable).  If you use a String instead, each modification will cause a new String to be allocated (since it's immutable).  So use a StringBuilder here.

If you have a string that you are not modifying then you should use a String - since it is immutable.

In practice most Strings in programming problems are not modified after they are created - hence why it's this way round (and we have immutable String and mutable StringBuilder and not a mutable String and an immutable StringConstant).

In general, use String everywhere, except where you are modifying the string.  Then use StringBuilder.

Make sense?

Doug
0
gudii9Author Commented:
and we have immutable String and mutable StringBuilder and not a mutable String and an immutable StringConstant

what is immutable stringconstant and where we use them and how they are related to string and string builder

In practice most Strings in programming problems are not modified after they are created - hence why it's this way round
i thought other way like we modify often. Why in real world the strings are not modified once they are created. Real world application do not need feature of modifying created string?
please advise
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ozoCommented:
In practice, all Strings in programming problems are not modified after they are created.
Instead, a new String is created with the modification.
This can be a relatively expensive operation, so if it is done often enough, using a mutable StringBuilder can be more efficient.
0
dpearsonCommented:
what is immutable stringconstant and where we use them and how they are related to string and string builder
There is no StringConstant.  I was just making a point, but I think I just confused you.  Please ignore that part of what I said, it's not very important.

Why in real world the strings are not modified once they are created. Real world application do not need feature of modifying created string?
Look for example at experts exchange - the web site.  It's got hundreds of thousands of strings in use - the vast majority of which are questions or comments that were typed in at one time and ever after they are fixed.  An old question or comment never changes.  So it's mutable only while somebody is typing it in and then immutable forever after.  Forever, being longer than the time it takes to edit a comment, means the vast majority of text (strings) on the site are immutable.

And this is a site where you can enter text.  Look at most web sites and you'll see no text being entered - no text changing.  It's just static blocks of text.  So immutable strings perform very well in these situations.
0
gudii9Author Commented:
Forever, being longer than the time it takes to edit a comment,

whenever i have to edit my post i use edit option. Then it has to be mutable righj?
please advise
0
dpearsonCommented:
whenever i have to edit my post i use edit option. Then it has to be mutable righj?
please advise
Sure - it's mutable while you are editing it but as soon as you hit save it goes back to being immutable.

Something like:

public String editComment(String originalComment) {
    StringBuilder newComment = new StringBuilder() ;
    newComment.append(originalComment) ;  // Make an editable version of the string
    // Edit the newComment here
   return newComment.toString() ;   // Turn it back into an immutable string for storage and display to others
}

Open in new window

The point is that editing and changing of things (strings in this case) is usually a lot less common, than reading and showing those things.  It's certainly true on almost all web sites if you think about it.  You can load and view the pages over and over and they only change relatively rarely (just like on experts exchange).  So it's usually a good idea to make most things immutable (like String) and only a few things mutable (like StringBuilder).

Doug
0
gudii9Author Commented:
public String editComment(String originalComment) {
    StringBuilder newComment = new StringBuilder() ;
    newComment.append(originalComment) ;  // Make an editable version of the string
    // Edit the newComment here
   return newComment.toString() ;   // Turn it back into an immutable string for storage and display to others
}

Open in new window


Turn it back into an immutable string for storage and display to others
it needs to be turned back to originalComment right. How to change it back to originalComment. So that if i have to change again i will edit it using above method.
0
dpearsonCommented:
t needs to be turned back to originalComment right. How to change it back to originalComment. So that if i have to change again i will edit it using above method.

String originalComment = editComment(originalComment) ;

Should do what you need.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Boomiraj PalrajJava DeveloperCommented:
String firstString = "Experts ";//New String object created for reference firstString
firstString = firstString.concat("exchange");//Here string object firstString is assigned to Experts exchange. And firstString object with value Experts is lost. so the String is immutable.

StringBuilder can be modified over and over again without leaving behind a great effluence of discarded String objects
0
gudii9Author Commented:
firstString = firstString.concat("exchange");//Here string object firstString is assigned to Experts

As above right and make firstString as "Experts exchange"
not as below
irstString = firstString.concat("exchange");//Here string object firstString is assigned to Experts exchange.
please advise
0
Boomiraj PalrajJava DeveloperCommented:
String firstString = "Experts ";
new String object, with a value of "Experts", and assign it to a reference variable firstString.
Once you have assigned a String a value, that value can never change— it's immutable.

firstString = firstString.concat("exchange");
couldn't stuff this new value  "exchange" into the old String referenced by firstString, so it created a new String object firstString, gave it the value "Experts exchange".
0
gudii9Author Commented:
got it. thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.