我请检查下面的代码。我可以理解未分配 String 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")); }}输出为: String s1 输出:Hello String s2 输出:Hello World
3 回答
小唯快跑啊
TA贡献1863条经验 获得超2个赞
这是因为s1
is Hello
whiles2.concat(" World")
是另一个string
具有连接字符串 as 的对象Hello World
。concat#string
方法总是返回string
带有连接字符串的对象的新实例。
添加回答
举报
0/150
提交
取消