传值与传址

package com.test1;

public class Test4 {
    public static void main(String[] args) {
        String a="123";
        test(a);
        System.out.println(a);
    }
    public  static void test(String str){
        str="456";
    }
}

输出为123

因为test(a)传递过去a的地址值给了str,然后str重新在字符常量池中创建一个"456",然后指向456,当方法结束后str就被销毁,a还是指向字符常量池中的"123"

posted @ 2015-11-03 11:41  vincent_duan  阅读(128)  评论(0编辑  收藏  举报