Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String, StringBuilder, StringBuffer differences....

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the exam, the difference between those three?
1) String is immutable, and others are not, use String, if your application isn't going to change the text frequently.
2) With the StringBuffer and StringBuilder, use StringBuffer, if your application is multi-threaded and use StringBuilder, if you application is working with single thread.

This OK, but other than this, What are the differences?

1) (+=) this kind of concatenation can't be used with other than String.
Example :

Please let us know the rest. Thanks in Advanced...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote: (+=) this kind of concatenation can't be used with other than String.



actually, you can use other object as operand with compound assignment operator. but *lefthand operand must be a String object*.
example you can do as mentiond below:


hth
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let us know the rest.

The thing about Strings being pooled is also important to know. Check this Journal article on Strings.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks....

I got this from here


Other than that, the two classes are remarkably similar with compatible API. It seems the author just copied StringBuffer.java to StringBuilder.java, removing all occurrences of "synchronized".


Please confirm!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the exam point of view, who cares how it was implemented ? The API tells you that "This class provides an API compatible with StringBuffer, but with no guarantee of synchronization.". This is enough. If you'er curious, you can check the source of both classes.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked, other than synchronization, are there any difference between StringBuffer and StringBuilder?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use StringBuffer, if your application is multi-threaded and use StringBuilder, if you application is working with single thread.

No, this is really not correct. It's perfectly fine to use StringBuilder in multi-threaded applications, as long as any such object is properly protected from concurrent access. In many (most?) cases, it will be implicitly protected from such access because StringBuilder (and -Buffer) instances are much more frequently declared within methods -where each thread would have its own copy on the stack- than outside of methods.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:It's perfectly fine to use StringBuilder in multi-threaded applications, as long as any such object is properly protected from concurrent access.

Agree.

Ulf Dittmer wrote: In many (most?) cases, it will be implicitly protected from such access because StringBuilder (and -Buffer) instances are much more frequently declared within methods -where each thread would have its own copy on the stack- than outside of methods.

though, StringBuilder uses important attribute called value, count directly from its super class AbstractStringBuilder in which declared as instance variable. obviously it would be problem in mutithreaded environment, but again it can be manageable with prober synchronized block explicity as you mentioned
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:StringBuilder uses important attribute called value, count directly from its super class AbstractStringBuilder in which declared as instance variable. obviously it would be problem in mutithreaded environment, but again it can be manageable with prober synchronized block explicity as you mentioned

If you read my post carefully you'll notice that I'm talking about fields declared within methods - so-called local fields. Those are thread-safe no matter how StringBuilder works internally, since each thread has its own instance.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:If you read my post carefully you'll notice that I'm talking about fields declared within methods - so-called local fields. Those are thread-safe no matter how StringBuilder works internally, since each thread has its own instance.

yes, I agree with that point . even StringBuffer implemented as StringBuilder exactly. I mean StringBuffer also has most of the fields declared within methods
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, Great conversation...... Could you guys do us a favor? Could you Please include some example coding with your concept, it's very difficult to us to understand, since we are newbie to this field.

Thanks in Advanced!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic