Solved

string vs string builder used in java

Posted on 2014-12-05
13
185 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
Comment Utility
0
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:dpearson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
got it. thank you
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

11 Experts available now in Live!

Get 1:1 Help Now