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 呢

解决方案 »

  1.   

    public class Test{
    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的地址没有发生改变。
      

  2.   

    class A
    {
        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);
        }
    }
      

  3.   

    class A
    {
        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);
        }
    }
      

  4.   

    哈,越是简单的就越复杂!
    这个是正解:
    Java在这里的确是地址传递,把对象的地址作为参数传递到了方法内部,但是对象s3和s1,以及对象s4和s2是不同的对象,只是这两组对象指向相同的地址罢了。把s2赋值给s1只是把s1的地址赋值给了s1,而s3的地址没有发生改变。
      

  5.   


    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
    }
    }
      

  6.   

    归根结底,Java中的一切传递都是“值传递”。
    LZ,如果把你的exchange(改成change或许更贴切些)改为如下:void exchange(A b, A c)
    {
        b.i = c.i;
    }就会如你所愿。
    为什么呢?方法中的形参只是其实参的副本(即the copied one)
    副本的改变不会影响原本,但其所指向的i都是同一个i,说白了,i并不是形参。
      

  7.   

    我对这个问题的想法:
    http://blog.csdn.net/jianzhizg/archive/2006/12/02/1427086.aspx
      

  8.   

    有没有这么复杂啊?
    传的时候产生了一个引用变量b
    b刚开始指向对像one.a1,然后b又指向one.a2,根本就没有改变什么啊,只是改变了b的引而已。