• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Any benefit by using StringBuffer like this?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When editing an old application, I see code like this:



As you can see, this is a Comparator subclass. It is used to sort Collection that could have thousands of MyClass objects.

In order to compare 2 MyClass objects, it makes use of their IDs, retrieved by the getId() method.

I just don't understand why it uses StringBuffer, then call StringBuffer.toString() to get another String instance with same content as the original one......

Does String has any synchronization or concurrency issue, so that using a separate instance gives better performance??

(The original programmer is unknown so I can't ask him why.......)
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Leung:

[CODE]
id1 = new StringBuffer(mc1.getId()); //getId() returns a Stringid2 = new StringBuffer(mc2.getId()); //getId() returns a Stringreturn id1.toString().compareTo(id2.toString());
[CODE]



If mc1.getId() returns string then store the id's in string and use compareTo direclty.Using string buffer in no case would improve the perfomance.
Taking a string , making a stringbuffer using the string and then again converting the string buffer into string before making use of it..I do not think it makes sense.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. This code seems unnecessarily long-winded. Simply comparing Strings ought to be fine.

The initialisation of the StringBuffer variables to null is a hint that the method may not have been written by the finest imaginable developer. It gains you nothing to initialise these variables in this way, and has two draw-backs. First, you deprive the compiler of the chance to check whether the variables are initialised to proper values in all code paths (*) and second, the unnecessary initialisation takes some time and some code space.

(*) Yes, I know your method has only one code paths, but it's a general point I'm trying to make.
 
Henry Leung
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both of you, then I will rewrite it in simpler style.

Especially Peter, I have never thought of the check-initialization-in-all-code-path thing. I will keep that in mind.
reply
    Bookmark Topic Watch Topic
  • New Topic