好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

从一道面试题看你对java的理解程度

简介

最近有点忙,很久没更新文章了,后面会慢慢恢复...回顾正题

最近看到一篇文章,关于一道 面试题 ,先看一下题目,如下:

?

1

2

3

4

5

6

7

8

9

10

11

public static void main(string[] args) {

  integer a = 1 ;

  integer b = 2 ;

  system.out.printf( "a = %s, b = %s\n" , a, b);

  swap(a, b);

  system.out.printf( "a = %s, b = %s\n" , a, b);

  }

 

public static void swap(integer a, integer b) {

  // todo 实现

}

有人可能在没经过仔细考虑的情况下,给出以下的答案

?

1

2

3

4

5

6

7

8

9

// 特别提醒,这是错误的方式

// 特别提醒,这是错误的方式

// 特别提醒,这是错误的方式

public static void swap(integer a, integer b) {

  // todo 实现

  integer temp = a;

  a = b;

  b = temp;

}

很遗憾,这是错误的。重要的事注释三遍

那么为什么错误,原因是什么?

想要搞清楚具体的原因,在这里你需要搞清楚以下几个概念,如果这个概念搞清楚了,你也不会把上面的实现方法写错

形参和实参 参数值传递 自动装箱

所以,上面的问题先放一边,先看一下这几个概念

形参和实参

什么是形参?什么是实参?概念上的东西,参考教科书或者google去吧,下面直接代码说明更加明显

?

1

2

3

4

5

6

7

8

9

public void test() {

  int shi_can = 0 ;

 

  testa(shi_can);

}

 

public void testa( int xing_can) {

 

}

注:为了清楚的表达意思,我命名的时候并没有按照java的驼峰规则命名,这里只是为了演示

通过上面的代码很清楚的表达形参和实参的概念,在调用testa时,传递的就是实参,而在testa方法签名中的参数为形参

从作用域上看,形参只会在方法内部生效,方法结束后,形参也会被释放掉,所以形参是不会影响方法外的

值传递和引用传递

值传递:传递的是实际值,像基本数据类型

引用传递:将对象的引用作为实参进行传递

java基本类型数据作为参数是值传递,对象类型是引用传递

实参是可以传递给形参的,但是形参却不能影响实参,所以,当进行值传递的情况下,改变的是形参的值,并没有改变实参,所以无论是引用传递还是值传递,只要更改的是形参本身,那么都无法影响到实参的。对于引用传递而言,不同的引用可以指向相同的地址,通过形参的引用地址,找到了实际对象分配的空间,然后进行更改就会对实参指向的对象产生影响

额,上面表述,可能有点绕,看代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

// 仅仅是一个java对象

public class inttype {

 

  private int value;

 

  public int getvalue() {

  return value;

  }

 

  public void setvalue( int value) {

  this .value = value;

  }

}

 

// main方法

public class inttypeswap {

  public static void main(string[] args) {

 

  // code_1

  inttype type1 = new inttype();

  type1.setvalue( 1 );

 

  inttype type2 = new inttype();

  type2.setvalue( 2 );

  // code_1

 

  swap1(type1, type2);

  system.out.printf( "type1.value = %s, type2.value = %s" , type1.getvalue(), type2.getvalue());

  swap2(type1, type2);

  system.out.println();

  system.out.printf( "type1.value = %s, type2.value = %s" , type1.getvalue(), type2.getvalue());

  }

 

  public static void swap2(inttype type1, inttype type2) {

  int temp = type1.getvalue();

  type1.setvalue(type2.getvalue());

  type2.setvalue(temp);

  }

 

  public static void swap1(inttype type1, inttype type2) {

  inttype type = type1;

  type1 = type2;

  type2 = type;

  }

}

在main方法中,code_1中间的代码为声明了两个对象,分别设置value为1和2,而swap1和swap2两个方法的目的是为了交互这两个对象的value值

先思考一下,应该输出的结果是什么

...

...

?

1

2

type1.value = 1 , type2.value = 2

type1.value = 2 , type2.value = 1

从输出结果来看swap1并没有达到目的,回头看一下

?

1

2

3

4

5

swap1public static void swap1(inttype type1, inttype type2) {

  inttype type = type1;

  type1 = type2;

  type2 = type;

  }

从值传递的角度来看,对象参数传递采用的是引用传递,那么type1和type2传递过来的是指向对象的引用,在方法内部,直接操作形参,交换了形参的内容,这样形参改变,都是并没有对实参产生任何影响,也没有改变对象实际的值,所以,结果是无法交换

而对于swap2,对象引用作为形参传递过来后,并没有对形参做任何的改变,而是直接操作了形参所指向的对象实际地址,那这样,无论是实参还是其他地方,只要是指向该对象的所有的引用地址对应的值都会改变

自动装箱

看我上面的那个例子的swap1,是不是顿时觉得与上面的面试题的错误做法非常相似了,是的,错误的原因是一模一样的,就是稍微有一点区别,就是integer不是new出来的,而是自动装箱的一个对象,那么什么是自动装箱呢?jdk到底做了什么事?

如果你不想知道为什么,只想知道结果,那么我就直说,自动装箱就是jdk调用了integer的 valueof(int) 的方法,很简单,看源码

?

1

2

3

4

5

public static integer valueof( int i) {

  if (i >= integercache.low && i <= integercache.high)

  return integercache.cache[i + (-integercache.low)];

  return new integer(i);

  }

上面那些如果不想深究可以忽略,就看最后一句,是不是明白了什么呢。没错,也是new出来一个对象,如果想知道上面的代码做了什么处理,可以参考 long==long有趣的现象 这篇文章,里面有介绍类似的

好了,有人可能会问,为什么会知道自动装箱调用的是valueof方法,这里其他人怎么知道的我不清楚,我是通过查看反编译的字节码指令知道的

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public static void main(string[] args) {

  integer a = 1 ;

  integer b = 2 ;

  system.out.printf( "a = %s, b = %s\n" , a, b);

  swap(a, b);

  system.out.printf( "a = %s, b = %s\n" , a, b);

  }

 

  public static void swap(integer a, integer b) {

  integer temp = a;

  a = b;

  b = temp;

  }

反编译出来的结果为

对比一下可以很清楚的看到 valueof(int) 方法被调用

回归

好,现在回归正题了,直接操作形参无法改变实际值,而integer又没有提供set方法,那是不是无解了呢?我很好奇如果有人以下这样写,面试官会有什么反应

?

1

2

3

4

public static void swap(integer a, integer b) {

  // todo 实现

  // 无解,

  }

既然出了肯定是有解的,可以实现,回头看看,在上面swap2的那个例子中是通过set方法来改变值的,那么integer有没有提供呢?答案没有(我没找到)

那就先看看源码

?

1

2

3

4

5

private final int value;

...

public integer( int value) {

  this .value = value;

  }

这是integer的构造函数,可以看到integer对象实际值是用value属性来存储的,但是这个value是被final修饰的,没办法继续找,value没有提供任何的set方法。既然在万法皆不通的情况下,那就只能动用反射来解决问题

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public static void swap(integer a, integer b) {

  int temp = a.intvalue();

  try {

  field value = integer. class .getdeclaredfield( "value" );

  value.setaccessible( true );

  value.set(a, b);

  value.set(b, temp);

 

  } catch (nosuchfieldexception e) {

  e.printstacktrace();

  } catch (illegalaccessexception e) {

  e.printstacktrace();

  }

  }

现在感觉很开心,终于找到解决方案,可是当你执行的时候,从输出结果你会发现,jdk在跟我开玩笑吗

?

1

2

a = 1 , b = 2

a = 2 , b = 2

为什么会出现这种情况,无奈,调试会发现是在 value.set 的时候将integer的缓存值改变了,因为 value.set(object v1, object v2) 两个参数都是对象类型,所以temp会进行自动装箱操作,会调用valueof方法,这样会获取到错误的缓存值,所以,为了避免这种情况,就只能不需要调用缓存值,直接 new integer 就可以跳过缓存,所以代码改成如下即可

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public static void swap(integer a, integer b) {

  int temp = a.intvalue();

  try {

  field value = integer. class .getdeclaredfield( "value" );

  value.setaccessible( true );

  value.set(a, b);

  value.set(b, new integer(temp));

 

  } catch (nosuchfieldexception e) {

  e.printstacktrace();

  } catch (illegalaccessexception e) {

  e.printstacktrace();

  }

  }

至此,这道题完美结束

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://juejin.im/post/5b8d21b651882542b20591af

查看更多关于从一道面试题看你对java的理解程度的详细内容...

  阅读:39次