Skip to main content

Always Use StringBuilder while concatenating Strings within loops

A common tendency of Java programmers is to always concatenate Strings using + operator. Which is actually very good, and simplifies the code by improves readability, since we would have to use StringBuilder.append(String), if the single + operator was not allowed.

In fact if we look in byte code generate from such concatenation style, we will see a StringBuilder being used to perform the action.

Check the JSL:  JLS

Now, the point is, although this facility, you should not use the + operator in loop concatenation.


Why?

A new StringBuilder Object will be constructed at every single loop iteration (with initial value of str) and at the end of every iteration there will be concatenation with initial String (actually StringBuilder with initial value of str).
So you need to create StringBuilder by yourself only when you work with String concatenation in loop.

Let us procuce the evidence


First, run this code, and see how long it takes to be executed:





Now, bellow is the code you should stick with, run it and evidence the improvement.

Notice the StringBuilder being created right beforer the for loop.

Needless to say...

you should not use the + operator in loop concatenation

Comments

Popular posts from this blog

The Scala's Equivalent of Java ArrayList

ArrayList   is a widely misused "default"  List  in Java. It has terrible performance when elements are added/removed frequently, but works pretty well as a replacement for  Array .  But what about Scala? What is Scala's default collection? What Scala collection has characteristics similar to  ArrayList ? What's a good replacement for  Array   in Scala? So here are the answers for these: What is Scala's default collection? Scala's equivalent of Java's  List  interface is the  Seq . A more general interface exists as well, which is the  GenSeq  -- the main difference being that a  GenSeq  may have operations processed serially or in parallel, depending on the implementation. Because Scala allows programmers to use  Seq  as a factory, they don't often bother with defining a particular implementation unless they care about it. When they do, they'll usually pick either Scala's List  or  Vector . They are both immutable, and  Vector

Java NIO.2 - The Path Class - An overview

The Java NIO.2 is a new API for file manipulation. Added to the Java Platform 7. This API comes with many benefits, and new classes. Those classes bring new functionalities, and solve problems of the traditional I/O API, as well as allow a more robust and simpler code. In this article, we are going to talk about one of the base classes: The Path class, present in the java.nio.File package. This class is basically one of the entry class of the Java NIO API. The models the Path of File or a directory in a File System in a particular Operational System - Microsoft Windows, for example. As we are a bout to see, this class offers many facilities that make it very important and present in other I/O Operations, as well as, it plays an important role with other classes in the NIO API. Let us get started in the journey of the Path class and its use. 1. Intancing a Path The Path, is actually an Interface, that is instanced with a static factory method from the utility class Paths(java.