码迷,mamicode.com
首页 > 其他好文 > 详细

String作为函数参数时,传入的是值,不是地址

时间:2016-04-24 17:00:12      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

写个小程序试下:

public class Example {

    String str = new String("good");

    char[] ch = { ‘a‘, ‘b‘, ‘c‘ };

    public static void main(String args[]) {

        Example ex = new Example();

        ex.change(ex.str, ex.ch);

        System.out.print(ex.str + " and ");

        System.out.print(ex.ch);

    }

    public void change(String str, char ch[]) {

        System.out.println("str:"+str+", ch:"+ch);
        str = "test ok";

        ch[0] = ‘g‘;

    }
}

运行结果:

str:good, ch:[C@de6ced
good and gbc

String作为函数参数时,传入的是值,不是地址

标签:

原文地址:http://www.cnblogs.com/lan-writenbook/p/5427320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!