java中引用传递例题_关于JAVA引用传递和值传递的题

publicclasspassingDemo{publicvoidfist(){xObjecto=newxObject();o.x=5;intx=5;changeThem(x,o);System.out.println();System.out.println("Backintheoriginalmethod");System.out.p...

public class passingDemo{

public void fist(){

xObject o=new xObject();

o.x=5;

int x=5;

changeThem(x,o);

System.out.println();

System.out.println("Back in the original method");

System.out.println("The value of o.x is " + o.x);

System.out.println("But.The value of x is now " + x);

}

public void changeThem(int x,xObject o){

x=9;

o.x=9;

System.out.println("In the changThem method");

System.out.println("The value of o.x is " + o.x);

System.out.println("The value of x is now " + x);

}

public static void main(String args[]){

passingDemo myDemo=new passingDemo();

myDemo.fist();

}

class xObject{

public int x=5;

}

}

输出结果

In the changThem method

The value of o.x is 9

The value of x is now 9

Back in the original method

The value of o.x is 9

But.The value of x is now 5

为什么输出顺序是从changeThem开始的。为什么first方法里的o.x变成9了

还有值传递和引用传递是怎么进行的。满意加分

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值