Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中连接字符串是否总是导致在内存中创建新字符串?_Java_String_Compiler Construction_String Concatenation - Fatal编程技术网

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

在Java中连接字符串是否总是导致在内存中创建新字符串?,java,string,compiler-construction,string-concatenation,Java,String,Compiler Construction,String Concatenation,我有一根很长的绳子,不适合屏幕的宽度。例如 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 longString = "This

我有一根很长的绳子,不适合屏幕的宽度。例如

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 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个新字符串,这可能会导致性能下降。是这样吗?或者编译器是否足够聪明,能够明白我所需要的只是一个字符串?我怎样才能避免这样做

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

不会的。由于这些是字符串文字,因此将在编译时对其进行计算,并且只创建一个字符串。这在以下文件中定义:

长字符串文字总是可以分解为较短的片段,并使用字符串连接运算符+
将其写入表达式(可能带括号) […]
此外,字符串文字总是引用类字符串的同一实例

  • 由常量表达式(§15.28)计算的字符串在编译时计算,然后将其视为文本
  • 在运行时通过连接计算的字符串是新创建的,因此是不同的
测试:

但是,下面的情况有所不同,因为它使用了一个变量-现在有了一个串联并创建了几个字符串:

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中连接字符串是否总是导致在内存中创建新字符串

不,它并不总是这样

如果连接是编译时常量表达式,则由编译器执行,并将生成的字符串添加到编译类常量池中。在运行时,表达式的值是与常量池条目相对应的插入的
字符串


这将发生在您问题的示例中。

请根据您的输入检查以下代码段:

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 ));
输出:

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

第一种情况:两个字符串相等(内容相同)


第二个大小写:显示串联后只有一个字符串所以只创建了一个字符串。

我想他是在问它是否会为
这个字符串创建一个字符串
这个字符串是
@CodeBlue是的,当且仅当您连接字符串文字时,才会创建一个字符串。@CodeBlue只有字符串文字被合并。在运行时创建的字符串不可用。除非您使用
intern()
方法强制他们进入池中。例如,在第二个代码段中,您可以尝试使用
stringother=(“这个字符串”+是+“很长”).intern()以查看差异。@akash746我不确定我是否理解您的问题。最好将其作为一个单独的问题和其他细节提问。值得注意的是,如果我们在第二个示例中将
final
修饰符添加到
is
,那么
“is”
将被视为文本,因此在输出中产生
true
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 ));