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

时光毁灭记忆、已成空白 提交于 2019-11-26 08:25:34

问题


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.\";

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?


回答1:


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
}



回答2:


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

No, it does not always do that.

If the concatenation is a compile-time constant expression, then it is performed by the compiler, and the resulting String is added to the compiled classes constant pool. At runtime, the value of the expression is the interned String that corresponds to the constant pool entry.

This will happen in the example in your question.




回答3:


Please check below snippet based on your inputs:

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.";

String longStringOther = "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.";

System.out.println(" longString.equals(longStringOther) :"+ longString.equals(longStringOther));      
System.out.println(" longString == longStringother : " + (longString == longStringOther ));

Output:

longString.equals(longStringOther) :true
longString == longStringother : true

1st Case : Both Strings are equal ( have same content)

2nd Case : Shows that there is only one String after concatenation. So only one String is created.



来源:https://stackoverflow.com/questions/11989261/does-concatenating-strings-in-java-always-lead-to-new-strings-being-created-in-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!