• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String Buffer Question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai please answer this question


public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append('<');
buffer.append(this.name);
buffer.append('>');
return buffer.toString();

which Statement is true???

a: this code is not thread safe..

b: the programmer can relace StringBuffer with String Builder with no other changes...

c: this cod will perform poorly. For better performance, the code should be re written as : "<"+this.name+">";

d: this code will perform well and converting the code to use StringBUilder will not enhance the performance...



thanks in advance/.....
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

B is correct ...

Good luck ...
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sandeep,

Please Quote Your Sources.

Thanks,
Henry
 
sandeep atluri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry..but i cant get the source of the code because it was given to me by one of my friends and he is not in contact with me...if get to meet or contact him.. i shall ask him about this question... thanks for your suggestion... i shall see to that its implemented./// thanks once again...
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Why isn't 'A' correct?"
StringBuffer is not thread safe!




Anybody please!!!

EDIT: Thanks Jim;
I missed:
"StringBuffer is thread safe; StringBuilder is not(its fast)".


Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um, all methods of StringBuffer are synchronized, so it is "thread-safe" - well, more than StringBuilder is anyway. However this is irrelevant because the StringBuffer is held only in a local variable, and there is no possible way for more than one thread to access that same StringBuffer at a time. This is the way StringBuffer is usually used, which is why StringBuilder is usually preferred - it offers the same functionality, an may be slightly faster.
 
I've been selected to go to the moon! All thanks to this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic