Link to home
Start Free TrialLog in
Avatar of maxmohave
maxmohave

asked on

Immutable String and when to use StringBuffer/StringBuilder versus String

I was taught that Java Strings are immutable. What this means to me is that if we do the following:

String product="radio";
product="DVD";

in your program memory you have someone still stored "radio" but you can't see it. But its sitting there wasting memory (until garbage cleanup). If you are detailing with same a million products it might be of concern. Below is my code solution where I use StringBuilder because I believe StringBuilder is cleaned up on the fly.

Question when should I use String versus StringBuffer or Builder (I understand that buffer is threadsafe but slightly less efficient than builder).  And does StringBuffer still have same memory problem as String?
Iterator it = productMap.keySet().iterator();
//Need to use key below, but since key may change over a million times
//use StringBuilder versus just a String?
while (it.hasNext()) {
   StringBuilder key = new StringBuilder(it.next().toString());
   productBean = (ProductBean)productMap.get(key.toString());
   if (productBean.getPriority().equals("1")) {
      availProductMap.put(key.toString(),productBean);
   } // end of checking for product still in use
}
 
//or should I just use the following:
 
while (it.hasNext()) {
   String key = new StringBuilder(it.next().toString());
   productBean = (ProductBean)productMap.get(key);
   if (productBean.getPriority().equals("1")) {
      availProductMap.put(key,productBean);
   } // end of checking for product still in use
}

Open in new window

Avatar of stevengmoreau
stevengmoreau

maxmohave,
Essentially you have answered your own question, since Strings are immutable setting a string to a million different values could cause quite a performance hit.

So, when you need to iterate with a string (or append) use StringBuilder, unless you require it to be thread safe, then use StringBuffer.
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are talking about 2 things here:
1. String is Immutable. This means that once you have created a instance of
   String class, you can't change it. Any change will lead to creation of
new
   string class. If you look at documentation there is no set method.
   Now, StringBuffer or StringBuilder is not Immutable, so, once you create
   instance of Stringbuffer, you can modify the elements of it.
 
   java.awt.Color is another example of Immutable class.
 
2. caching of string constants:
   String product = "radio";
   Here the value radio is cached. Like any other caching it has certain
limit
   and timeout after which it will be collected.
Avatar of maxmohave

ASKER

All three answers were good, but I guess what I was really looking for was a best practice answer. The answer that I chosed as correct indicated that either way I approached my problem I was going to be creating strings over and over. Main thing is the toString method creates a string anyway, so I don't gain anything.
Thanks everyone.
Oops,

sorry about the B grade. CPColin answered perfectly, I wish I could go back and change grade. I was clicking through too fast.

max