class test
{
public static void main(String[] args)
{
String str="hello";
str="world";
System.out.print(str);
}
}
字符串是常量,一旦被赋值,就不能被改变,上述代码运行结果为world,这是为什么
Java String一旦赋值就不能被改变,那为什么最后输出结果是world
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
threenewbee 2023-03-30 23:43关注str="world";是创建了一个新的字符串,内容是world,然后str这个引用指向新的字符串,原来的丢弃了。并没有修改
评论 打赏 举报解决 2无用