跨行学java学的怀疑人生_你的Java功底有多深?一道面试题,让我怀疑人生

本文探讨了一道Java面试题,涉及值传递与引用传递的概念。通过示例代码解释了Integer对象在-128到127之间时的自动缓存问题,以及如何使用反射和栈来解决交换值的问题。最后讨论了不同方法的优缺点,并提出了针对-127和128之间数值交换的挑战。
摘要由CSDN通过智能技术生成

你的Java功底有多深?一道面试题,让我怀疑人生。

上一次帅杰出去面试,面试官给他出了这样一道题。

题目:通过swap交换两个integer类型的值。

01:我们一般会怎么写

public static void main(String[] args) {

Integer a=128;

Integer b=129;

swap(a,b);

System.out.println("a="+a+",b="+b);

}

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

int temp=a;

a=b;

b=temp;

}

}

打完收工。帅杰这样写的,然后早早交了答案,结果面试机会都没有争取到。

看一下打印出来的结果:

a=128,b=129。

我明明交换了两个值,怎么没有打印出来,这时候就涉及到我们所说的引用传递和值传递

Java中的数据类型分为两种为基本类型和引用类型

1、基本类型的变量保存原始值,所以变量就是数据本身。

常见的基本数据类型

byte,short,int,long,char,float,double,Boolean

2、引用类型的变量保存引用值,所谓的引用值就是对象所在内存空间的“首地址值”,通过对这个引用值来操作对象。

我们写个类测试下:

public class Swap2 {

String str = new String("hello");

char[] ch = {'a', 'b'};

public static void main(String[] args) {

Swap2 ex = new Swap2();

ex.change(ex.str, ex.ch);

System.out.println(ex.str + " and");

System.out.println(ex.ch);

}

public void change(String str, char[] ch) {

str = "ok";

ch[0] = 'c';

}

}

打印结果:hello and

cb

02:那么我们的面试题要怎么做?

可以说大家都知道反射技术是Java中很强的技术,我们可不可以使用反射来解决这道题。

话不多说,上代码

public static void main(String[] args) {

Integer a=128;

Integer b=129;

swap(a,b);

System.out.println("a="+a+",b="+b);

}

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

int temp=a;

try {

Field filed=Integer.class.getDeclaredField("value");

filed.setAccessible(true);

filed.set(a,b);

filed.set(b,temp);

} catch (Exception e) {

e.printStackTrace();

}

}

我们看打印结果:

a=129,b=128

打印出来的结果完全正确。利用Integer class的对象,获取里面的value值的对象,并交换两个value值。写到这是不是就结束了?。

03:我们简单把代码做一下改造。

public class Swap {

public static void main(String[] args) {

Integer a=1;

Integer b=2;

swap(a,b);

System.out.println("a="+a+",b="+b);

}

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

int temp=a;

try {

Field filed=Integer.class.getDeclaredField("value");

filed.setAccessible(true);

filed.set(a,b);

filed.set(b,temp);

} catch (Exception e) {

e.printStackTrace();

}

}

}

我们看打印结果:a=2,b=2

看到a的值换过来了,b的值没换。那是为什么呢?

看Integer类的源码。(涉及到自动装箱,拆箱的知识)

public static Integer valueOf(int i) {

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

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

return new Integer(i);

}

我们看到low的值类里面定义的是-128,high的值为127

private static class IntegerCache {

static final int low = -128;

static final int high;

static final Integer cache[];

static {

// high value may be configured by property

int h = 127;

原来在-128和127之间的值是从缓存来的,我们方法虽然定义了交换变量temp,但是最后temp和a都指向了同一个地址。a的值变化为2,temp的值也变换为2。

04:那么怎么改进

我们可以这样写

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

int temp=a;

try {

Field filed=Integer.class.getDeclaredField("value");

filed.setAccessible(true);

filed.set(a,b);

filed.set(b,new Integer(temp));

} catch (Exception e) {

e.printStackTrace();

}

}

重新生命一个Integer堆对象。这样新声名的对象和原来的指向不同的地址。

或者这样写。

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

int temp=a;

try {

Field filed=Integer.class.getDeclaredField("value");

filed.setAccessible(true);

filed.set(a,b);

filed.setInt(b,temp);

} catch (Exception e) {

e.printStackTrace();

}

}

分析SetInt方法的源码底层也一样声名了两个不同的引用地址。

05:如果面试不用中间变量我们该怎么办

用栈队列貌似可以实现

public static void main(String[] args) {

Integer a=1;

Integer b=2;

Stack stack= new Stack();

stack.push(a);

stack.push(b);

a = (int)stack.pop();

b = (int)stack.pop();

// swap(a,b);

System.out.println("a="+a+",b="+b);

}

我们用异或的方法是不是也可以实现

public static void main(String[] args) {

int a = 1, b =2;

a = a ^ b;

b = a ^ b;

a = a ^ b;

// swap(a,b);

System.out.println("a="+a+",b="+b);

}

06:有些人喜欢吹毛求疵,你这没有用到引用传递和值传递的相关知识?

帅杰说他明白了,让他自己写一个,他是这样写的

public class Swap {

public static void main(String[] args) {

Integer a=128;

Integer b=129;

swap(a,b);

System.out.println("a="+a+",b="+b);

}

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

// int temp=a;

try {

Stack stack= new Stack();

stack.push(a);

stack.push(b);

Field filed=Integer.class.getDeclaredField("value");

filed.setAccessible(true);

System.out.println(filed.get(128));

System.out.println(filed.get(129));

filed.set(a,(int)stack.pop());

filed.set(b,(int)stack.pop());

} catch (Exception e) {

e.printStackTrace();

}

}

}

打印结果:a=129,b=129

原来他把stack.push(a);

stack.push(b);

由于Integer类型是引用传递。造成第二次给b赋值的时候还是129

最后帅杰做了改进

public class Swap {

public static void main(String[] args) {

Integer a=128;

Integer b=129;

swap(a,b);

System.out.println("a="+a+",b="+b);

}

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

// int temp=a;

try {

Stack stack= new Stack();

stack.push(new Integer(a));

stack.push(new Integer(b));

Field filed=Integer.class.getDeclaredField("value");

filed.setAccessible(true);

System.out.println(filed.get(128));

System.out.println(filed.get(129));

filed.set(a,(int)stack.pop());

filed.set(b,(int)stack.pop());

} catch (Exception e) {

e.printStackTrace();

}

}

}

07:那么如果针对-127和128之间呢?

d4da4918d7598198ad32cb016009526c.png

由于Java不能操作地址来操作指针,这个时候通过这种方法,就找不到b的引用地址了。导致a和b永远相等。这个时候传Integer类型就是值传递了。除非我们去改Integer源码,把缓存去掉,这样才能实现这个方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值