You can't modify the String. What you are doing is creating a new String. goes out of scope quickly.Kamalika Seenivasan wrote:. . . Strings are immutable, so does this mean a new object is created every time I modify the string?
Adversely. You won't notice anything until your loop runs several thousand times; in Java9 (2019) the implementation of the loop was changed; I have forgotten the details, but such a loop run about 100× faster than it had done in Java8. You will still find it is painfully slow to concatenate 100,000 objects, and using a StringBuilder runs thousands of times faster. A StringBuilder runs even fasterif you can estimate the size of the final result and enter a size for the backing array into its constructor.. . . How does this impact performance in large loops?
No. If any of the operands to the + operator in the following code is a String, the repeated us of + is probably the best way to concatenate everything:-
Should I always use StringBuilder instead of String for concatenation?
That form of code allows the javac tool to apply whichever optimisation is currently programmed as the best at the moment.. . . a simple explanation . . .
[/Warning]
The part you are interested strats with line 14. You will see it calls a static method on this class which creates aCallSite object, and that is as much as I know about it. See if you can understand the details. In Java8 or previous, you can use the following instructions:-campbell@campbellCampbellsComputer$ javac StringConcatenator.java
campbell@campbellsComputer$ java StringConcatenator CodeRanch is brilliant .
Concatenated Strings:-
CodeRanchisbrilliant.
(You have to change var to String in the code, line 10. Ignore the warning messages.)You will then see that the loop uses a StringBuilder object (line 30), which is created every time the loop runs; later it goes out of scope and has to be deleted by the garbage collector program whenever you start to run low on heap memory. That is much slower than the newer technique.javac -source 8 --target 8 StringConcatenator.java
javap -c StringConcatenator
The problem with getting rid of the "undesirables" is that sooner or later someone will decide that YOU are an undesirable.
The problem with getting rid of the "undesirables" is that sooner or later someone will decide that YOU are an undesirable.
A long time ago, I was told that using plain simple + is proably the best way to implement String concatenation outwith any loops because the JVM or javac can implement whichever optimisation is fastest, if optimisation is an issue at all.Mike Simmons wrote:. . . times when using string concatenation by + or += . . . may be faster . . .

Do you know he difference between those two types?waqas sb wrote:. . . in loops—use StringBuilder or String Buffer . . .
waqas sb wrote:For performance, avoid repeated concatenation in loops
Campbell Ritchie wrote:A long time ago, I was told that using plain simple + is proably the best way to implement String concatenation outwith any loops
| Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |