请看如下题目:
public class testAddress {
   
private static  Integer a=new Integer(5);
private static Integer b=new Integer(6); public static void swap(Integer c,Integer d){
Integer temp;
temp=c;
d=temp;
c=d;
}
public void output(){
System.out.println("a="+a+"\tb="+b);
}
public static void main(String[] args) {

       testAddress add=new testAddress();
         
       add.output();
       swap(add,a,b); 
       add.output();
}} 输出结果都是一样,请高手发表下看法;

解决方案 »

  1.   

    你定义成static的了,把static去掉结果就不一样了。
      

  2.   

    本来就是一样的 因为java是值传递 而不是引用传递 有什么问题么?
    另外你写的好像有点问题啊
      

  3.   

    c,d是a,b的引用副本。
    a,b所引用的没啥变化。
      

  4.   

    如你所说:
      
    public class testAddress {
       
    private  Integer a=new Integer(5);
    private  Integer b=new Integer(6);
    private static String val="boss"; public static void swap(Integer c,Integer d,String value){
    Integer temp;
    temp=c;
    d=temp;
    c=d;

    val=value;
    }
    public void output(){
    System.out.println("a="+a+"\tb="+b+"\tval="+val);
    }
    public static void main(String[] args) {

           testAddress add=new testAddress();
             
           add.output();
           swap(add.a,add.b,"employee"); 
           add.output();
    }}
    不过增加一个String 类型以作参考,
    结果是:
    a=5 b=6 val=boss
    a=5 b=6 val=employee
      

  5.   

    从java中堆,栈的角度去讨论这个问题.
      

  6.   


    请问LZ 你这不给val重新赋值了吗当然变了 val=value;
      

  7.   

    只有传对象类型才是传引用..基本类型的封装类在作为参数传递的时候,视为基本数据类型.所以上面的是传值.
    应该是这样..!  自己去试下.!反正我已经调过了.. Integer是不会改value的
      

  8.   

    Java编程语言中只有值传递参数 你这种写法对象的内容是没有改变的。
    为了使对象的内容改变 可以这样写:
    public class testAddress {
       
        private   Integer a=new Integer(5);
        private   Integer b=new Integer(6);    public static void swap(testAddress c){
          Integer temp;      temp=c.a;
          c.a=c.b;   
      c.b=temp;
        }
        public void output(){
            System.out.println("a="+a+"\tb="+b);
        }
        public static void main(String[] args) {
        
           testAddress add=new testAddress();
             
           add.output();
           swap(add); 
           add.output();
        }}
      

  9.   

    有两个问题:
    1. 这里的交换本身就存在问题。你认真看看?
            Integer temp;
                  temp=c;
                  d=temp;
                  c=d;2. 声明了static变量就表示是不能再修改其地址了.
      

  10.   

       java中没有引用传递??
         有,但引用传递的参数类型一定要是自己自定义的类型
      

  11.   

       Integer temp; 
       temp=c; 
       d=temp; 
       c=d; 写对了吗?
    传对象类型是传引用基本类型的封装类在作为参数传递的时候,为基本数据类型,是传值
      

  12.   

    这个题目涉及很多问题:
    1.
    楼主的这个写的有问题: 
     Integer temp;
      temp=c;
      d=temp;
      c=d; 
    2.
    关于传递的问题,除了基本类型传递的时候是值传递之外,其他的类型,也就是各种对象在传递的时候都是采用引用传递。但是必须注意,传递只是传递一个引用的副本。
    3.
    Integer类型里面的内容是保持不变的,这个跟string有点类似,比如有语句:Integer ite=new Integer(3);ite=5;
    这个时候ite开始存放的是Integer(3)的内存地址,后来有存放的是5的内存地址,原来的Integer(3)变成垃圾会被回收。所以,即使上面的交换写的争取,c和d的交换不会影响a和b原来的值,所以结果还是一样的
      

  13.   

    java的引用传递本质上还是值传递 java并没有真正意义上的引用传递 传递的只是引用的值而已
      

  14.   


    public static void swap(Integer c,Integer d){
            Integer temp;
            temp=c;
            d=temp;
            c=d;    
        }
    这个错了。
    还有说明一下:Integer c,Integer d,当你传进参数时产生了两个新的Integer对象,这两个对象拷贝了原来两个对象的值,这样你对他进行交换根本就没有影响到原来的内存区域,当然不会出现你想要的结果了。
    你想实现交换的话可能通过自己定义的类型去实现~
      

  15.   

    //值不变
        public static void swap1(Apple a,Apple b){
          Apple c;
          c=a;
          a=b;
          b=c;
         }
    //值改变
         public static void swap(Apple a,Apple b){
          Apple c=new Apple();
          c.setcolor(a.getcolor());
          c.setname(a.getname());
             a.setcolor(b.getcolor());
             a.setname(b.getname());
             b.setcolor(c.getcolor());
             b.setname(c.getname());
     
         }
        Apple apple1=new Apple();
        Apple apple2=new Apple();
        swap1(apple1,apple2);
        swap(apple1,apple2);
    swap1中两个Apple对象进去a指向apple1 b指向apple2 它们相当四个不同指针
    通过交换后只是把b指向apple1,a指向apple2;apple1 和apple2依然不会变
    而swap中把apple1 和apple2传进去 a与apple1指向同一段内存 b与apple2指向通一段内存
    当你修改a的值时 因为apple1和它指向的是同一段内存所以值就会变