Java中的参数都是传值,那怎么交换两个变量的值呢。
最近我碰到一个经典问题,写一个函数Swan(a,b),要求交换a,b的值。
因为Java是不支持指针和传址,我想来想去,感觉这个函数还没法子简单的写出来了。
Core Java是这么说明参数传递的原则的:
A method cannot modify a parameter of primitive type (that is, numbers or Boolean values).
A method can change the state of an object parameter.
A method cannot make an object parameter refer to a new object.