您的位置:首页 > 其它

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

2015-11-24 17:21 239 查看
基本数据类型:传递的是 值 本身—栈中操作

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

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
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: