class A
{
int i;
A(int i)
{this.i = i;}
}class test
{
A a1 = new A(1);
A a2 = new A(2);
void exchange(A b, A c)
{ b = c; }
public static void main(String[] args)
{
test one = new test();
one.exchange(one.a1, one.a2);
System.out.println(one.a1.i);
}
}exchange传递的是A对象的引用
在函数里面应该是已经把
a2赋值给了a1
此时a1实际上是a2的引用了
a1的i应该等于2为什么打印出来的结果是 1 呢
{
int i;
A(int i)
{this.i = i;}
}class test
{
A a1 = new A(1);
A a2 = new A(2);
void exchange(A b, A c)
{ b = c; }
public static void main(String[] args)
{
test one = new test();
one.exchange(one.a1, one.a2);
System.out.println(one.a1.i);
}
}exchange传递的是A对象的引用
在函数里面应该是已经把
a2赋值给了a1
此时a1实际上是a2的引用了
a1的i应该等于2为什么打印出来的结果是 1 呢
解决方案 »
- 在直角坐标系平面内一点有P(2,4),求OP与x轴正半轴的夹角
- 请问一个JB里的小问题,希望没有发错版
- Component和JComponent有什么关系?
- 加急!!!对数据进行分类的问题(在线等解)
- 我是想连接打印机打印但点了print button 后打印界面就出来了 但不能完成打印 后果是会一直运行无法退出除非重启电脑
- 再提供30分,寻求答案!!(共计130分)
- 如何存一个文件在当前目录下??(win, unixs)
- 请问什么api可以限制JTextField和JTextArea的最大输入字数?急
- java applet的问题?
- 如何在LINUX下设置数据源?
- java 如何操作剪切板?
- Socket流的建立
public static void main(String[] args){
StringBuffer s3 = new StringBuffer("1");
StringBuffer s4 = new StringBuffer("2");
swap(s3,s4);
System.out.println (s3.hashCode());
System.out.println (s4.hashCode());
}
public static void swap(StringBuffer s1,StringBuffer s2){
System.out.println (s1.hashCode());
System.out.println (s2.hashCode());
s1 = s2;
System.out.println (s1.hashCode());
System.out.println (s2.hashCode());
}
}Java在这里的确是地址传递,把对象的地址作为参数传递到了方法内部,但是对象s3和s1,以及对象s4和s2是不同的对象,只是这两组对象指向相同的地址罢了。把s2赋值给s1只是把s1的地址赋值给了s1,而s3的地址没有发生改变。
{
int i;
A(int i){this.i = i;}
}class Test12
{
A a1 = new A(1);
A a2 = new A(2); void exchange(A b1, A b2)
{
int b = b1.i;
b1.i = b2.i;
b1.i = b;
System.out.println("b1:" + b1.i);
System.out.println("b2:" + b2.i);
} public static void main(String[] args)
{
Test12 test = new Test12();
test.exchange(test.a1, test.a2);
System.out.println("a1:" + test.a1.i);
System.out.println("a2:" + test.a2.i);
}
}
{
int i;
A(int i){this.i = i;}
}class Test12
{
A a1 = new A(1);
A a2 = new A(2); void exchange(A b1, A b2)
{
int b = b1.i;
b1.i = b2.i;
b2.i = b;
System.out.println("b1:" + b1.i);
System.out.println("b2:" + b2.i);
} public static void main(String[] args)
{
Test12 test = new Test12();
test.exchange(test.a1, test.a2);
System.out.println("a1:" + test.a1.i);
System.out.println("a2:" + test.a2.i);
}
}
这个是正解:
Java在这里的确是地址传递,把对象的地址作为参数传递到了方法内部,但是对象s3和s1,以及对象s4和s2是不同的对象,只是这两组对象指向相同的地址罢了。把s2赋值给s1只是把s1的地址赋值给了s1,而s3的地址没有发生改变。
class A
{
int i;
A(int i)
{this.i = i;}
}class test
{
A a1 = new A(1);//a1=1
A a2 = new A(2);//a2=2void exchange(A b, A c)
{ b = c; } public static void main(String[] args)
{
test one = new test();
one.exchange(one.a1, one.a2); //a1=a2 其实就是把a1的值赋给a2
System.out.println(one.a1.i); //所以显示的值还是1
}
}
LZ,如果把你的exchange(改成change或许更贴切些)改为如下:void exchange(A b, A c)
{
b.i = c.i;
}就会如你所愿。
为什么呢?方法中的形参只是其实参的副本(即the copied one)
副本的改变不会影响原本,但其所指向的i都是同一个i,说白了,i并不是形参。
http://blog.csdn.net/jianzhizg/archive/2006/12/02/1427086.aspx
传的时候产生了一个引用变量b
b刚开始指向对像one.a1,然后b又指向one.a2,根本就没有改变什么啊,只是改变了b的引而已。