zw929 2023-03-30 23:40 采纳率: 50%
浏览 13

Java String一旦赋值就不能被改变,那为什么最后输出结果是world


class test
{
    public static void main(String[] args)
    {
        String str="hello";
        str="world";
        System.out.print(str);
    }
}
字符串是常量,一旦被赋值,就不能被改变,上述代码运行结果为world,这是为什么
  • 写回答

2条回答 默认 最新

  • threenewbee 2023-03-30 23:43
    关注

    str="world";是创建了一个新的字符串,内容是world,然后str这个引用指向新的字符串,原来的丢弃了。并没有修改

    评论

报告相同问题?

问题事件

  • 创建了问题 3月30日