• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Concatenation Finally

 
Vikas Aggarwal
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved from servlets to Java In General (Intermediate)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic