• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String vs StringBuilder

 
Ranch Hand
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 5
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, so what are the benefits of StringBuilder over StringBuffer?

Regards,
Dan
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 88
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm...this discussion is getting interesting. Thanks everyone
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic