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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

StringBuilder and StringBuffer

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hey , guis , what will be the correct answer ,and why

[edited] - this was a real exam question !!!

Do NOT post real exams questions at JavaRanch - if you somehow get a copy of a real exam question it is, ILLEGAL! There is no way a copy of a real exam question can be legal.

Thanks,

Bert

[end of edit]
[ August 05, 2007: Message edited by: Bert Bates ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I think B is correct.

A is not the right answer I think since the StringBuffer class is synchronized.
C will make a whole lot of unused String object as result of (+)-String operator, so that is not better for performance
D StringBuilder might enhance a very little performance since is is not synchronized

B is true since the StringBuilder and StringBuffer classes have the same methods.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi option 'c' is also right taking into consideration - performance in terms of responsiveness - not in terms of memory utilization.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That's not true, Kishore.

The Java compiler converts code like "<" + this.name + ">" to code with StringBuffer as shown in the question behind the scenes. So in the end, the bytecode will be (approximately) the same.

If you want proof of this, write two small Java programs (one with the + operator and the other with StringBuffer), compile them and then disassemble them with javap (a tool included with the JDK). You will see that even in the program with the + operator, a StringBuffer object is used to do the actual concatenation of the strings.
[ August 05, 2007: Message edited by: Jesper Young ]
 
Get me the mayor's office! I need to tell him about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic