突然想到java是按值传递参数的,那么常常在c c++(允许按值和引用传递参数)中看到的数值交换,怎么可以在java中实现呢?
自己想了个小demo测试下,可以.
public class Test
{
public int a;
public int b;
public void swap(int a,int b)
{
this.a=b;
this.b=a;
}
public static void main(String[] args)
{
int a=2,b=3;
System.out.println(a);
System.out.println(b);
Test t=new Test();
t.swap(a, b);
System.out.println(t.a);
System.out.println(t.b);
}
}结果:
2
3
3
2
原文地址:http://blog.csdn.net/u013470102/article/details/45537649