【发布时间】:2019-02-12 15:29:43
【问题描述】:
我请检查下面的代码。
我可以理解没有分配字符串 s1,因此即使使用了 concat(string) 方法,它也会给出原始输出。
但是,在 String s2 的情况下,没有分配变量,但连接有效。
谁能解释一下?
package com.stringconcat.main;
public class StringConcat {
public static void main(String[] args) {
String s1 = "Hello";
s1.concat(" World");
System.out.println("String s1 output: " + s1);
String s2 = "Hello" /*s1*/;
System.out.println("String s2 output: " + s2.concat(" World"));
}
}
输出是: 字符串 s1 输出:你好 字符串 s2 输出:Hello World
【问题讨论】:
-
System.out.println("String s2 output: " + s2.concat(" World"));基本上是String temp1 = "String s2 output: "; String temp2 = s2.concat(" World "); System.out.println(temp1 + temp2);。 -
谢谢哥们!!对不起,我的错,我错过了!!
-
好的,伙计们!!!我的坏,我的错误!很抱歉我错过了它,并感谢那些只关心消除混乱并尽其所能提供帮助的好人!如果有人生气,我再次道歉。如果这没有帮助你自己!
-
@AbhiroopNandiRay 认真地删除此评论。您将被标记并很可能从 stackoverflow 中删除,此外这很粗鲁......就像超级粗鲁
标签: java string immutability concat