当前位置:网站首页>In-depth understanding of the auto-increment operator from two error-prone written test questions
In-depth understanding of the auto-increment operator from two error-prone written test questions
2022-07-31 00:15:00 【come on】
目录
自增运算符:
不是原子操作;
前缀自增自减法(++a,--a): 先进行自增或者自减运算,再进行表达式运算;
后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算;
int i = 0; i = i++;
0: iconst_0 | 把int型的0值压入操作数栈 |
1: istore_1 | Store a value from the operand stack into the local variable table |
2: iload_1 | Load a local variable onto the operand stack |
3: iinc 1by 1 | iinc是对int类型的值进行自增操作,后面第一个数值1表示,局部变量表的index值,说明要对此值执行iinc操作,第二个数值1表示要增加的数值.(这时局部变量表index为1的值因为执行了自增操作变为1了,但是操作数栈中栈顶的值仍然是0) |
6: istore_1 | Store a value from the operand stack into the local variable table |
7: iload_1 | Load a local variable onto the operand stack |
从执行顺序可以看到,这里第1和第6执行了2次将0赋值给变量i的操作(=号赋值),i++操作是在这两次操作之间执行的,自增操作是对局部变量表中的值进行自增,而栈顶的值没有发生变化,这里需要注意的是保存这个初始值的地方是操作数栈而不是局部变量表,最后再将栈顶的值覆盖到局部变量表i所在的索引位置中去.
x = -1;
y = x++ + ++x;
x 执行 x++ ,when performing an addition operationx = -1 (先运算,后自增),此时 x = 0,++x 结果是1(先自增,后运算),becomes larger-1 + 1,所以 y = 0;
例题一:
检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果.
package algorithms.com.guan.javajicu;
public class Inc {
public static void main(String[] args) {
Inc inc = new Inc();
int i = 0;
inc.fermin(i);
i= i ++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}正确答案: 0
It can be confusing to look at it aloneint i = 0; i = i++;这句代码,使用jclasslib查看字节码;

0: iconst_0 | 把int型的0值压入操作数栈 |
1: istore_1 | Store a value from the operand stack into the local variable table |
2: iload_1 | Load a local variable onto the operand stack |
3: iinc 1by 1 | iinc是对int类型的值进行自增操作,后面第一个数值1表示,局部变量表的index值,说明要对此值执行iinc操作,第二个数值1表示要增加的数值.(这时局部变量表index为1的值因为执行了自增操作变为1了,但是操作数栈中栈顶的值仍然是0) |
6: istore_1 | Store a value from the operand stack into the local variable table |
7: iload_1 | Load a local variable onto the operand stack |
这里第1和第6执行了2次将0赋值给变量i的操作(=号赋值),i++操作是在这两次操作之间执行的,自增操作是对局部变量表中的值进行自增,而栈顶的值没有发生变化,这里需要注意的是保存这个初始值的地方是操作数栈而不是局部变量表,最后再将栈顶的值覆盖到局部变量表i所在的索引位置中去.
而fermin方法中的iBecause it is not a reference variable,So it will not change the originali;最终答案是0
例题二:【阿里巴巴-笔试】
以下代码的输出结果是?【阿里巴巴-笔试】
public class Test{
static{
int x=5;
}
static int x,y;
public static void main(String args[]){
x--;
myMethod( );
System.out.println(x+y+ ++x);
}
public static void myMethod( ){
y=x++ + ++x;
}
}prints:3
静态语句块中x为局部变量,不影响静态变量x的值
x和y为静态变量,默认初始值为0,属于当前类,其值得改变会影响整个类运行
y = x++ + ++x;
x 执行 x++ ,when performing an addition operationx = -1 (先运算,后自增),此时 x = 0,++x 结果是1(先自增,后运算),becomes larger-1 + 1,所以 y = 0,x = 1;
然后x+y+ ++x 等于3;
边栏推荐
- Dry goods | 4 tips for MySQL performance optimization
- What are the efficient open source artifacts of VSCode
- Homework: iptables prevent nmap scan and binlog
- 软考学习计划
- C# VSCode & Rider引用命名空间快捷键
- IOT跨平台组件设计方案
- HCIP第十六天笔记
- 【唐宇迪 深度学习-3D点云实战系列】学习笔记
- .NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
- Data cleaning - ingest using es
猜你喜欢

【唐宇迪 深度学习-3D点云实战系列】学习笔记

align-content、justify-content、align-items三个属性的作用和效果

SWM32系列教程6-Systick和PWM

joiplay模拟器rtp如何安装

天空云变化案例

2022 China Logistics Industry Conference and Entrepreneur Summit Forum will be held in Hangzhou!

作业:iptables防止nmap扫描以及binlog

Chevrolet Trailblazer, the first choice for safety and warmth for your family travel

47. 【Pointers and Arrays】

从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
随机推荐
How to solve the error of joiplay simulator
Machine Learning 1-Regression Model (2)
实验7(MPLS实验)
第一个独立完成的千万级项目
限制字符绕过
会议OA项目待开会议、所有会议功能
uniapp折叠框二级循环
HCIP Day 15 Notes
Kotlin特殊类
Point Cloud Scene Reconstruction with Depth Estimation
MPI简谈
Axure轮播图
【深入浅出玩转FPGA学习13-----------测试用例设计1】
leetcode:127. 单词接龙
Necessary artifacts - AKShare quants
DATA AI Summit 2022提及到的对 aggregate 的优化
边缘计算与小程序也能结合!智能家居是否能借势上台阶
360核心安全大脑3.0正式发布,构建政企用户的“能力中枢平台”
状态机动态规划之股票问题总结
encrypted transmission process