当前位置:网站首页>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;
边栏推荐
- Android安全性优化——APP加固
- Steven Giesel recently published a 5-part series documenting his first experience building an application with the Uno Platform.
- How to import game archives in joiplay emulator
- PHP图片添加文字水印
- Word文件损坏如何修复
- 状态机动态规划之股票问题总结
- 游戏商城表建立
- Installation considerations for pytorch
- DNS解析过程【访问网站】
- (五)fastai应用
猜你喜欢

DATA AI Summit 2022提及到的对 aggregate 的优化

How to solve types joiplay simulator does not support this game

封装、获取系统用户信息、角色及权限控制

天空云变化案例

uniapp开发微信小程序-软考刷题小程序

An easy-to-use interface testing tools - the Postman

Optimization of aggregate mentioned at DATA AI Summit 2022

image里的mode属性

Machine Learning 1-Regression Model (2)

46.
随机推荐
Soft Exam Study Plan
MySQL的grant语句
leetcode:127. 单词接龙
binglog日志追踪:数据备份并备份追踪
Dry goods | 4 tips for MySQL performance optimization
uni-ui installation
Machine Learning 1-Regression Model (2)
How to solve the error of joiplay simulator
firewalld
神经网络(ANN)
mysql中关于存储过程无法实现迁移复制表中数据问题
【萌新解题】删除链表的倒数第 N 个结点
uni-ui安装
【VisDrone数据集】YOLOV4训练VisDrone数据集步骤与结果
MPI简谈
【深入浅出玩转FPGA学习14----------测试用例设计2】
HCIP Day 15 Notes
transition过渡&&animation动画
Shell脚本 if语句
对象集合去重的方法