Java fastest way to concatenate strings, integers and floats

java, string-concatenation

Solution

That should already be fast - it'll use `StringBuilder` internally for concatenation. Arguably using `StringBuilder` explicitly could eliminate the concatenation of empty strings, but it's not likely to make a big difference.

How often are you doing this, anyway? It must be pretty often, for it to be a bottleneck... do you really need to do it that often?

EDIT: For those who are saying "Use StringBuilder, it'll be faster" - consider this code:

public class Test
{
    public static void main(String[] args)
    {
        int x = 10;
        int y = 20;
        int z = 30;
        String foo = x + "," + y + "," + z + ";";
        System.out.println(foo);
    }
}

Compile that, then use `javap -c` to see what the compiler generates...

Problem

What is the most performant way to build strings from strings, integers and floats? currently I'm doing this and it uses a lot of cpu time. ``` String frame = this.frameTime + ":" + this.player.vertices[0].x + "," + this.player.vertices[0].y + "," + this.player.activeAnimId + "," + (int)this.player.virtualSpeed + "," + this.map.getCurrentTime() + (this.player.frameSound == -1 ? "" : "," + this.player.frameSound) + (this.player.frameDecal.equals("") ? "" : "," + this.player.frameDecal) + ";"; ``` Is there a way to do this faster?

Original source