Solved

string vs string builder used in java

Posted on 2014-12-05
13
186 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 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40484208
0
 
LVL 26

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
 
LVL 84

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 26

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

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 26

Accepted Solution

by:
dpearson earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now