面试题----入参两个Integer,无返回值,然后使这个两个值在调用函数后交换

我最近看到过一个比较好玩的面试题。

写个方法,入参两个Integer,无返回值,然后使这个两个值在调用函数后交换

很有意思的一个题目,引发我的深思,根据一路的学习过来,下面把实现代码贴出来,方便学习。

这里我们需要注意的是值传递,还是引用传递。

import java.lang.reflect.Field;

public class TestSwap {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Integer a=1;
        Integer b=2;
        System.out.println("**********转换前**********");
        System.out.println("a="+a);
        System.out.println("b="+b);

        swapChangeAwithB(a,b);
        System.out.println("**********转换后**********");
        System.out.println("a="+a);
        System.out.println("b="+b);
    }

    //声明
    // 以下是java.lang.Class.getDeclaredField()方法的声明
    // public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
    // 参数
    // name -- 这是字段的名字。
    // 返回值
    // 这个方法返回这个类中的指定域的域对象。

    public static  void swapChangeAwithB(Integer a ,Integer b ) throws NoSuchFieldException, IllegalAccessException {
        //通过反射获取Integer对象中的私有域
        //value这个值就对应的是Integer对象中的value属性
        Field field = Integer.class.getDeclaredField("value");
        field.setAccessible(true);

        int temp = a.intValue();

        //调用set(obj,value)方法
        //obj表示要修改的对象,value表示要给修改对象赋予的值
        field.set(a,b);
        field.set(b,new Integer(temp));
    }
}

打印的结果 :

 

posted @ 2018-10-09 17:33  Kmily_Lee  阅读(414)  评论(0编辑  收藏  举报