基本数据类型与引用数据类型传递

来源:互联网 发布:淘宝女装店推荐 编辑:程序博客网 时间:2024/05/22 14:14

基本数据类型:传递的是 值 本身—栈中操作
引用数据类型: 应用,不是 值 本身 —堆中操作(可修改)

Demo1:

    public class PassValue{        public static void main(String[] args){            PassValue pv = new PassValue();            int x = 5;            System.out.println("方法调用之前x==" + x);//5            pv.change(x);//100            System.out.println("方法调用之后x==" + x);//5          }        public void change(int y){//y = 5;            y = 100;            System.out.println("方法中y==" + y);//100        }    }

Demo2:

public class PassValue2{        private int x ;        public static void main(String[] args){            PassValue2 obj = new PassValue2();            obj.x = 5;            System.out.println("方法调用之前obj.x==" + obj.x);//5            obj.change(obj);//100            System.out.println("方法调用之后obj.x==" + obj.x);//100         }        public void change(PassValue2 obj2){            obj2.x = 100;            System.out.println("方法中obj2.x==" + obj2.x);//100        }    }
0 0