62,620
社区成员
发帖
与我相关
我的任务
分享
static void changeValue(String s1, String s2) {
s1 = s1 + s2;
System.out.println(s1 + "\t" + s2);
s2 = s1.substring(0,s1.length()-s2.length());
System.out.println(s1 + "\t" + s2);
s1 = s1.substring(s2.length(),s1.length());
System.out.println(s1 + "\t" + s2);
}
package csdn;
class A {
public Object value = 0;
public A() {
}
public A(Object i) {
value = i;
}
}
public class CSDNTest {
public static void fun(A x, A y) {
Object temp = x.value;
x.value = y.value;
y.value = temp;
}
public static void main(String[] args) {
int x = 1, y = 2;
A ax = new A(x);
A ay = new A(y);
System.out.println("ax = " + ax.value + "," + "ay = " + ay.value);
fun(ax, ay);
System.out.println("ax = " + ax.value + "," + "ay = " + ay.value);
}
}
x和y的值是无法通过函数交换的,如果你希望有两个数据的值能交换,那么你就要将它们作为类A的成员,因为a.value是对value的引用,通过引用就能交换数据的值