天天看点

java值传递与引用传递

看代码:

  

import java.lang.reflect.Field;

public class Test {

    public static void main(String[] args) {
        Integer a=1000;
        Integer b=890;
        System.out.println("a="+a+",b="+b);
        swap(a,b);
        System.out.println("a="+a+",b="+b);
    }
    private static void swap(Integer a,Integer b){
        try {
            int tempA=b.intValue();
            int tempB=a.intValue();
            Field field=Integer.class.getDeclaredField("value");
            field.setAccessible(true);
            field.setInt(a, tempA);
            field.setInt(b, tempB);
            System.out.println("a="+tempA+",b="+tempB);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}