aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String, StringBuilder, StringBuffer differences.... 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 "String, StringBuilder, StringBuffer differences...." Watch "String, StringBuilder, StringBuffer differences...." New topic
Author

String, StringBuilder, StringBuffer differences....

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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...


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please let us know the rest.

The thing about Strings being pooled is also important to know. Check this Journal article on Strings.


[My Blog]
All roads lead to JavaRanch
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Nov 04, 2009
Posts: 2066

I asked, other than synchronization, are there any difference between StringBuffer and StringBuilder?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
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.


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 04, 2009
Posts: 2066

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String, StringBuilder, StringBuffer differences....