aspose file tools*
The moose likes Java in General and the fly likes String Concatenation Finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Concatenation Finally" Watch "String Concatenation Finally" New topic
Author

String Concatenation Finally

Vikas Aggarwal
Ranch Hand

Joined: Jun 22, 2001
Posts: 140
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
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Moved from servlets to Java In General (Intermediate)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547

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
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Concatenation Finally