JavaRush /Java Blog /Random EN /String += String or StringBuilder.append(String)?
SemperAnte
Level 4
Донецк

String += String or StringBuilder.append(String)?

Published in the Random EN group
In this article, I would like to consider, analyze, tell and show what is the difference between the append method from the class StringBuilderand the operator +=for String. Here the question will be not so much in areas of application as in code optimization.
String += String or StringBuilder.append(String)?  - 1
Yes, a person who has undoubtedly not gone deep into this question will say: “Why should I replace the += operator by creating a new object of a completely different class, and then call the method as well toString()? What kind of optimality are we talking about? The choice is obvious, what are you talking about?” and he would be completely wrong. One of the problems is that Stringit is not primitive. String- an object like any other classin Java, and as you know, in Java there is no such thing as operator overloading (As in C++, for example), operators are defined only for primitives, and for any class we cannot override any operator. That is why the operators " +" and " += " are a kind of "crutch" of the Java library, and a crutch always incurs losses. Actually, let's stop pulling the cat's tail and move on to measurements. Here is a simple program that measures the time it takes to “glue” a line with a loop variable 100,000 times.
public class MainClass
{
    private static long time;

    public static void main(String[] args)
    {

        saveTime();
        String s = "Hello";
        for(int i = 0; i < 100000; ++i)
        {
            s+=i;
        }
        printDiff();

    }
    private static void saveTime()
    {
        time = System.currentTimeMillis();
    }

    private static void printDiff()
    {
        System.out.println((System.currentTimeMillis() - time) + "ms");
    }
}
On my computer, 6815ms was displayed in the console. That is, it took my computer almost 7 seconds to stitch this line together. Now let's replace it with StringBuilder and even include the method toString()in the measurements.
public class MainClass
{
    private static long time;

    public static void main(String[] args)
    {

        saveTime();
        StringBuilder sb = new StringBuilder("Hello");
        for(int i = 0; i < 100000; ++i)
        {
            sb.append(i);
        }
        String s = sb.toString();
        printDiff();

    }
    private static void saveTime()
    {
        time = System.currentTimeMillis();
    }

    private static void printDiff()
    {
        System.out.println((System.currentTimeMillis() - time) + "ms");
    }
}
My PC told me 10ms. That is, 0.01 Seconds. I think the difference in practice is quite obvious; roughly speaking, it worked 700 times appendfaster. This is due to the fact that most likely " +" and " +=" can themselves call the same append, but at the same time having gone a long way through the crutches of the language in order to understand that such an operator even exists and what it should do (the last paragraph is nothing more than a guess , I’m not a JVM developer and I don’t know what’s there and how). This is a clear example of the fact that an extra object is not always costly. Yes, the code will become a couple of lines longer, but the time savings in large projects can be colossal. Please note that the measurements were not made on an office PC with a prehistoric processor, but now imagine what a difference it will make on this very office computer, which can hardly pull the scarf.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION