• 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

String Concatenation Finally

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This has probably been discussed many times. But I have categorised my doubts. I have lots of old servlets in which "+" operator has been used thousand times. On the profile it shows that string objects are the highest in number.

They have been used like this:

out.println("My Test Info "+info+" Is Not Recent. "+testNumber"+ Is Wrong. This needs to be "+newTestNumber+"");

The alternatives that I have found are:

A. Use StringBuffer

But that meakes perceived page response time to be slower. Also it is not very possible to replace all "+" operator from all servlets (400 servlets) to have StringBuffer.
But if it has to be done then we will spend the effort and time.

B. Use StringBuilder

Same as String Buffer with added complexity of Sychronisation issues

C. Do something like this:

out.println("My Test Info ");out.print(info);out.print("Is Not Recent. ");out.print(testNumber);out.print("+ Is Wrong. This needs to be ");out.print(newTestNumber);

Is there any other method to minimise the string object creation in my case? Which one of above three is better and faster.

Thanks.

Vikas Aggarwal
 
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 Vikas Aggarwal:

out.println("My Test Info "+info+" Is Not Recent. "+testNumber"+ Is Wrong. This needs to be "+newTestNumber+"");



I do not think there is anything wrong in this.The slice at which you have profiled your application might have had a lots of string objects.
As long as you do not hold any unnecessay references to those objects , thats ok.When required GC can collect the memory back.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved from servlets to Java In General (Intermediate)
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Same as String Buffer with added complexity of Sychronisation issues


Not if you use it in a local scope (only one Thread accesses it at the same time)

But that meakes perceived page response time to be slower


Why do you know that this was because of your changes ? May be it was because of the network ? The Database ? The Weather ?

Another solution for your List:
- You could use JSPs


pascal
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikas Aggarwal:

Is there any other method to minimise the string object creation in my case? Which one of above three is better and faster.



In any non-trivial Java program there will be an alarming number of String objects in circulation. Lucky for us, the JVM is optimized to handle short-term object generation and collection. As for your alternatives, I doubt you would be able to tell the difference between your current code and what you've proposed. A or B would not make any difference, as they merely mimic exactly what goes on within the VM (see here) . You mention "synchronization issues" with a StringBuilder. I'm not sure what you mean here. Servlet code should be written with multiple threads in mind. Synchronizing servlet code is a Bad Idea as the web server would only be able to service one client at a time. Synchronization at the StringBuffer/builder level wouldn't make sense in a servlet, as that implies multiple requests would write to the same buffer. Your buffer should be local to the servlet method. Each request would get its own instance and you don't need to synchronize.
As for C, the application server is likely buffering output so you're just pushing down the construction of the string one layer and introducing numerous method invocations to do it.
Optimization is a tricky business and there are few hard and fast rules. That said, object generation is probably the least of your problems, at least with the minuscule amount of code you've shown us.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic