aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String vs StringBuilder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String vs StringBuilder" Watch "String vs StringBuilder" New topic
Author

String vs StringBuilder

Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

ok please help me to understand this two things...."String" and "StringBuilder". Important point is, both are Object. In addition to that StringBuilder is not synchronized means not thread safe. By the way, the only major thing (code wise) which differenciate these two are as follows:

For String (creating 2 reference objects, 1 reference variable):


Note: Although JVM will create an anonymous object with value "abcdef" at line number2, but that will distroy immediately (but will remain in the memory). And therefore in line number 3 we will see the output as "abc"

For StringBuilder (creating 1 reference object, 1 reference variable) <---- I am not sure:


Note: Here, JVM will hold the memory and will modify again and again the same String object

That's how StringBuilder bits the String object. Am i right? If there is some thing more important points are missing here then please add.

On the other hand, I know that String object is immutable. But here are not we changing it's value while using StringBuilder class? Or again here that instance of Object concept is leading the floor? Can any one please help me to make this concept clear?
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
That's how StringBuilder bits the String object. Am i right?


These both and StringBuilder have their place.

But here are not we changing it's value while using StringBuilder class?


That's it. That's the reason it's called StringBuilder.

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Randy Hernandez
Greenhorn

Joined: Nov 03, 2011
Posts: 5

Hi Faisal Fuad

String objects are immutable, it means that:

String str = "text";

is equivalent to:

char data[] = {'t', 'e', 'x', 't'};

and you can't change it. The reason String objects are immutable is related to the String constant pool, it makes Java more memory efficient when two references points to the same String. In my opinion StringBuilder is a whole different story, it acts as a buffer with a fixed capacity, once that buffer overflows, it is automatically made larger.

Both are useful , Strings for normal character manipulation and StringBuilder for file I/O streams or large blocks of changing characters.

Cheers
beepath
Aniket S. Kulkarni
Ranch Hand

Joined: Jun 15, 2011
Posts: 87

Randy Hernandez welcome to JavaRanch!!!
You are right Randy Hernandez.

StringBuilder:Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

String:The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created.

This link may help you: String vs StringBuilder


Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Great, so what are the benefits of StringBuilder over StringBuffer?

Regards,
Dan
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

StringBuffer is thread-safe. That comes at a cost, and in most cases where people use StringBuffer/StringBuilder, they don't actually need it to be thread safe (how often you you have more than one thread writing to the same StringBuffer?). If you don't need the thread-safety, use a StringBuilder as it will be slightly faster.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Thank you Matthew.

So, this is interesting, the relation, let's say of a certain Java program and the underlying threads. Ok, so I have a program that processes data via a StringBuffer or StringBuilder. How many threads would a program like this use?

Regards,
Dan
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
In other words, how can I be sure that my block of code that uses StringBuilder is indeed thread-safe, if the code doesn't contain any synchronized statements, either explicitly or implicitly (as in StringBuffer)?

Regards,
Dan
Faisal Fuad
Ranch Hand

Joined: Jul 05, 2011
Posts: 79

hmm...this discussion is getting interesting. Thanks everyone
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String vs StringBuilder