值传递和引用传递区别

来源:互联网 发布:电玩游戏java实战 编辑:程序博客网 时间:2024/06/05 18:08

1.值传递是指一个参数传递给一个函数时,函数接收的是原始值得副本。

2.引用传递是一个参数传递给一个函数时,函数接收的是原始值的地址。

3.java应用程序中,有且仅有一种传递,即值传递。

4.对象是按引用传递的。

public class Test {    String str = new String("good");    char[] ch = { 'a', 'b', 'c' };    public static void main(String args[]) {        Test ex = new Test();        //执行了这一句之后        ex.change(ex.str, ex.ch);        //exstr属性没变,但是ch属性被修改了        System.out.print(ex.str + " and ");        System.out.print(ex.ch);    }    public void change(String str, char ch[]) {        str = "test ok";        ch[0] = 'g';    }}
输出为:good and gbc


0 0
原创粉丝点击