Does concatenating strings in Java always lead to new strings being created in memory?

I realized that the second way uses string concatenation and will create 5 new strings in memory and this might lead to a performance hit.

No it won’t. Since these are string literals, they will be evaluated at compile time and only one string will be created. This is defined in the Java Language Specification #3.10.5:

A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator +
[…]
Moreover, a string literal always refers to the same instance of class String.

  • Strings computed by constant expressions (ยง15.28) are computed at compile time and then treated as if they were literals.
  • Strings computed by concatenation at run-time are newly created and therefore distinct.

Test:

public static void main(String[] args) throws Exception {
    String longString = "This string is very long.";
    String other = "This string" + " is " + "very long.";

    System.out.println(longString == other); //prints true
}

However, the situation situation below is different, because it uses a variable – now there is a concatenation and several strings are created:

public static void main(String[] args) throws Exception {
    String longString = "This string is very long.";
    String is = " is ";
    String other = "This string" + is + "very long.";

    System.out.println(longString == other); //prints false
}

Leave a Comment