aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Which is better in String or StringBuilder  and in which perspectives? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Which is better in String or StringBuilder  and in which perspectives?" Watch "Which is better in String or StringBuilder  and in which perspectives?" New topic
Author

Which is better in String or StringBuilder and in which perspectives?

Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Which is better in String or StringBuilder and in which perspectives?


Regards
Salil Verma
Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56
String is immutable - Cannot be changed after creation
StringBuilder is mutable - Can be changed after creation hence suitable forappend, insertion and other modification operations. This class is unsynchronized which is it's advantage over StringBuffer.

Use StringBuilder when the string is going to be manipulated and at most only one thread will be accessing it.


SCJP 1.4
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Vivek,

Thank but then I am quite curious to know, why the String was introduced in java language and in which type of situations we should use String rather than StringBuilder.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Normally, when dealing with text, you should use String. There are good reasons why it is immutable - for example, this makes it much easier to make concurrent programs and makes optimizations such as string pooling possible.

Use StringBuilder when you need to build up text, for example by repeatedly appending things. Have a look at this example:

This code is very inefficient, because of the repeated String concatenations in the loop. For each iteration, a new String object will be made, for which the content of 'result' until that point will have to be copied. So this is what happens:

i = 1: result = new String object containing "A, B"
i = 2: result = new String object, first "A, B" is copied into it and then ", C" is appended
i = 3: result = new String object, first "A, B, C" is copied into it and then ", D" is appended
i = 4: result = new String object, first "A, B, C, D" is copied into it and then ", E" is appended
i = 5: ...

You see that every time the result until now is copied into a new String object - that's very inefficient. You can avoid this by using StringBuilder:

StringBuilder is mutable, it only needs to append ", " and names[i] in each iteration and it doesn't need to copy the whole result each time - so it's much more efficient.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Jesper,

There are good reasons why it is immutable - for example, this makes it much easier to make concurrent programs and makes optimizations such as string pooling possible.


Could you please explain a real time scenario where String provide better results due to the immutable property.

Piyush Porwal
Ranch Hand

Joined: Apr 09, 2008
Posts: 30
I like the ideas thrown in this article if that helps you. It basically says that it may really doesn't matter, what class you use, in terms of performance.

http://www.codinghorror.com/blog/archives/001218.html


-Piyush Porwal (Junk box)
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hey Piyush,

Thanks for the article.


1: Simple Concatenation 606 ms
2: String.Format 665 ms
3: string.Concat 587 ms
4: String.Replace 979 ms
5: StringBuilder 588 ms


This again proves the point that StringBuilder is better than String and Concat function call is better than using "+" (String concatenation operator)..

My question still remain unanswered. Is there any real time situation whether we should recommend using String class and + operator for concatenation in place of StringBuilder .

Regards
Salil Verma




Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

My question still remain unanswered. Is there any real time situation whether we should recommend using String class and + operator for concatenation in place of StringBuilder .


This question seems to imply that the String and StringBuilder types are interchangable. They are clearly not.

If you are going to do a lot of string processing (creating of strings), then StringBuilder is probably a better candidate over Strings. The obvious exception is probably regular expressions, as the regex engine is better tied to Strings than the StringBuilder class. Regardless, even when you use the "+" operator on strings, the Java compiler will translate it to use string builders for you, so it is not that "inefficient".

However, if your methods are going to take parameters, it should be strings -- since you probably don't want to be passed something that can change (by another thread) while you are processing it. If you are going to return values, it should be strings -- or at least, a COPY of your string builders, as you don't want them to be mutated by the caller to your method.

It is this exact second reason that most of the builtin API takes strings, and not stringbuilders -- particulary the API that needs the security manager. It is impossible to check the validity of a request, if the request can change.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Thanks Henry,
This definitely make sense
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is better in String or StringBuilder and in which perspectives?
 
Similar Threads
string question
Binary Attachments
String, String Buffer and String Builder
Line break
String and StringBuffer