• 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
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

String, StringBuilder, StringBuffer differences....

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked, other than synchronization, are there any difference between StringBuffer and StringBuilder?
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic