在 Java 中连接字符串是否总是会导致在内存中创建新字符串?

我有一个不适合屏幕宽度的长字符串.例如.

I have a long string that doesn't fit the width of the screen. For eg.

String longString = "This string is very long. It does not fit the width of the screen. So you have to scroll horizontally to read the whole string. This is very inconvenient indeed.";

为了方便阅读,我想到了这样写——

To make it easier to read, I thought of writing it this way -

String longString = "This string is very long." + 
                    "It does not fit the width of the screen." +
                    "So you have to scroll horizontally" +
                    "to read the whole string." +
                    "This is very inconvenient indeed.";

但是,我意识到第二种方法使用字符串连接,会在内存中创建 5 个新字符串,这可能会导致性能下降.是这样吗?或者编译器是否足够聪明,可以找出我真正需要的只是一个字符串?我怎么能避免这样做呢?

However, 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. Is this the case? Or would the compiler be smart enough to figure out that all I need is really a single string? How could I avoid doing this?

推荐答案

我意识到第二种方式使用字符串连接,会在内存中创建 5 个新字符串,这可能会导致性能下降.

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.

不,不会的.由于这些是字符串文字,它们将在编译时进行评估,并且只会创建一个字符串.这是在 Java 语言中定义的规范#3.10.5:

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:

一个长字符串总是可以被分解成更短的部分,并使用字符串连接运算符 +
写成一个(可能带括号的)表达式[...]
此外,字符串字面量总是引用 String 类的同一个实例.

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.

  • 由常量表达式(第 15.28 节)计算的字符串在编译时计算,然后将其视为文字.
  • 在运行时通过串联计算的字符串是新创建的,因此是不同的.

测试:

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
}

相关文章