来道不一样的 Java 面试题
Posted 泰 戈 尔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了来道不一样的 Java 面试题相关的知识,希望对你有一定的参考价值。
问题描述
int a = 2;
int b = a++ << ++a + ++a;
问:此时a、b 的值分别是多少?
先来发一下答案:1024
。是不是很惊讶?接下来一点点来看下,为什么会得到这个答案。
知识储备:
1 运算符优先级 +
++
<<
参考链接 https://www.cnblogs.com/gw811/archive/2012/10/13/2722752.html
2 查看字节码
参考链接 https://segmentfault.com/a/1190000003871183
本次借助 IDEA 查看 Java 字节码 。
逐步攻克
- 来一版最简单的
public class HelloWorld
public static void main(String[] args)
int a = 2;
int b = a++;
System.out.println(b); // 2
字节码反编译结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class HelloWorld
public HelloWorld()
public static void main(String[] args)
int a = 2;
byte var10000 = a;
int var3 = a + 1;
int b = var10000;
System.out.println(b);
- 运算符编译优化处理
public class HelloWorld
public static void main(String[] args)
int a = 2;
int b = ++a + ++a;
System.out.println(b); // 7
字节码反编译结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class HelloWorld
public HelloWorld()
public static void main(String[] args)
int a = 2;
int a = a + 1;
int b = a++ + a;
System.out.println(b);
- 朝着题头问题前进
public class HelloWorld
public static void main(String[] args)
int a = 2;
int b = 1 << ++a + ++a;
System.out.println(b); // 128
字节码反编译结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class HelloWorld
public HelloWorld()
public static void main(String[] args)
int a = 2;
int a = a + 1;
int b = 1 << a++ + a;
System.out.println(b);
- 题头问题复现
public class HelloWorld
public static void main(String[] args)
int a = 2;
int b = a++ << ++a + ++a;
System.out.println(b); // 1024
字节码反编译结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class HelloWorld
public HelloWorld()
public static void main(String[] args)
int a = 2;
int a = a + 1;
++a;
int b = a << a++ + a;
System.out.println(b);
- 拓展左移操作符
public class HelloWorld
public static void main(String[] args)
int a = 2;
int b = ++a << ++a;
System.out.println(a); // 4
System.out.println(b); // 48
字节码反编译结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class HelloWorld
public HelloWorld()
public static void main(String[] args)
int a = 2;
int a = a + 1;
int b = a++ << a;
System.out.println(a);
System.out.println(b);
- 再遇难题
public class HelloWorld
public static void main(String[] args)
int a = 2;
int b = ++a << (++a + ++a);
System.out.println(a); // 5
System.out.println(b); // 1536 == 6 << 8
字节码反编译结果
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
public class HelloWorld
public HelloWorld()
public static void main(String[] args)
int a = 2;
int a = a + 1;
int b = a++ << a++ + a;
System.out.println(a);
System.out.println(b);
面试过程中,遇到此类问题,可能没啥争议,但是工作时组内谁要是写这样的代码,恐怕会被吐槽的吧。
以上是关于来道不一样的 Java 面试题的主要内容,如果未能解决你的问题,请参考以下文章
2021大厂Android面试高频100题最新汇总(附答案详解)