Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string vs string builder used in java

Posted on 2014-12-05
13
Medium Priority
?
223 Views
Last Modified: 2014-12-19
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
0
Comment
Question by:gudii9
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 85

Assisted Solution

by:ozo
ozo earned 1000 total points
ID: 40484208
0
 
LVL 28

Expert Comment

by:dpearson
ID: 40484339
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
 
LVL 7

Author Comment

by:gudii9
ID: 40486790
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 40486864
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
 
LVL 28

Expert Comment

by:dpearson
ID: 40488335
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
 
LVL 7

Author Comment

by:gudii9
ID: 40488899
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
 
LVL 28

Expert Comment

by:dpearson
ID: 40489710
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
 
LVL 7

Author Comment

by:gudii9
ID: 40497049
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
 
LVL 28

Accepted Solution

by:
dpearson earned 1000 total points
ID: 40497240
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
 

Expert Comment

by:Boomiraj Palraj
ID: 40506366
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
 
LVL 7

Author Comment

by:gudii9
ID: 40507261
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
 

Expert Comment

by:Boomiraj Palraj
ID: 40508604
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
 
LVL 7

Author Comment

by:gudii9
ID: 40509126
got it. thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question