aspose file tools
The moose likes Java in General and the fly likes String concatenation performance problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "String concatenation performance problem" Watch "String concatenation performance problem" New topic
Author

String concatenation performance problem

Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
Hey Guys!
I want to know the difference between


Is line 2 better in performance than line 1 or they are same? Should one use StringBuffer for cases like line 2.

Thanks!
Martin
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
A few things.

First the code you wrote looks like VB, not Java.

Performance, in situations like your example, should be looked at with a profiler after the code is finished.

The compiler will probably optimise some of that away, unless they are all actually variables rather than literals.

After all that I really don't think there is much of a differance.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As far as speed, I don't know that there is any difference. However, both of these examples will use much more space than StringBuffer will. To understand why, let's look at the second example:

First a new String "12" is formed from "1" + "2". Then the String "123" is formed by concatenating the "3", and so on. So overall 60 Strings will be created, one for each of the individual numbers and one for each of the intermediate results of the concatenation. On the other hand StringBuffer will use a single buffer and concatenate the characters to it dynamically.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Steven Bell:
A few things.

First the code you wrote looks like VB, not Java.

I think the above was meant as pseudocode. With only a little fleshing out, it would be perfect Java.

Layne
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note: If you're really worried about performance (and not worried about thread synchronization), then Java 1.5's StringBuilder is intended to be even more efficient than StringBuffer.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Don't forget compiler-optimization!
using once:

and another time:

results in exactly the same class.

Using Stringbuffer (or ~Builder) is mostly the better solution, together with a loop, since it is more easy to modify.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: String concatenation performance problem
 
Similar Threads
Strings
Difference between String and StringBuffer.
how can we make class "immuatble" explicitly
Passing a String reference to a method.
Strings