Ryan511 2016-08-24 14:23 采纳率: 100%
浏览 2138
已采纳

一个String值不会改变问题

 class Ref{
    String temp = "Hello";
}
public class StringProblem {


    public static void main(String[] args) {
        String str1 = "hello";
        System.out.println(str1);//输出"hello"
        change(str1);
        System.out.println(str1);//输出"hello"
        Ref ref = new Ref();
        System.out.println(ref.temp);//输出"Hello"
        change(ref);
        System.out.println(ref.temp);//输出"world"
    }
    public static void change (String str ){
        str = "world";
    }
    public static void change (Ref str ){
        str.temp = "world";
    }
}

为什么str1的值不会修改 ref.temp的值会修改?

  • 写回答

11条回答 默认 最新

  • li_tieyang 2016-08-24 15:20
    关注

    图片说明
    自己分析

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?