连接Java中的字符串总是导致在内存中创建新的字符串? [英] Does concatenating strings in Java always lead to new strings being created in memory?

查看:199
本文介绍了连接Java中的字符串总是导致在内存中创建新的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  String longString =此字符串非常长,不适合屏幕宽度,因此您必须水平滚动读取整个字符串,这是非常不方便的。 

为了更容易阅读,我想到这样写 -

  String longString =这个字符串很长。 + 
它不适合屏幕的宽度。 +
所以你必须水平滚动+
才能读取整个字符串。 +
这是非常不方便的。但是,我意识到第二种方法使用字符串连接,并将在内存中创建5个新字符串,并且这个可能会导致性能下降。是这样的情况吗?或者编译器能够聪明地找出我需要的只是一个单一的字符串?我如何避免这样做?

解决方案


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


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


一个长字符串常量可以分解为较短的部分,并写成字符串连接运算符+

[...]

此外,字符串常量总是引用类String的同一个实例。




  • 通过常量表达式计算的字符串(第15.28节)在编译时计算,然后作为文字处理。

  • 在运行时通过连接计算的字符串是新创建的,因此是不同的。


测试:

  public static void main(String [] args)throws Exception {
String longString =这个字符串很长。
String other =这个字符串+是+很长。

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

但是,下面的情况不同,一个变量 - 现在有一个连接和几个字符串创建:

  public static void main(String [] args)throws Exception {
String longString =这个字符串很长。
String is =is;
String other =这个字符串+是+很长。

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


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?

解决方案

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
}

这篇关于连接Java中的字符串总是导致在内存中创建新的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆