当前位置:网站首页>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;
边栏推荐
- C# VSCode & Rider引用命名空间快捷键
- mysql 中手动设置事务提交
- Manually set transaction commit in mysql
- Dry goods | 4 tips for MySQL performance optimization
- Unity 加载读取PPT
- ctfshow 文件包含
- software development design process
- firewalld
- 消息队列存储消息数据的MySQL表设计
- Ukraine's foreign ministry: wu was restored to complete the export of food security
猜你喜欢
随机推荐
Dry goods | 4 tips for MySQL performance optimization
从编译的角度来学作用域!
What are the efficient open source artifacts of VSCode
Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。
How to use joiplay emulator
How to solve the error of joiplay simulator
47. 【Pointers and Arrays】
【VisDrone数据集】YOLOV4训练VisDrone数据集步骤与结果
Jetpack Compose学习(8)——State及remeber
pytorch双线性插值
MySQL面试题
Machine Learning 1-Regression Model (2)
How to adjust Chinese in joiplay simulator
二叉查找树的定义,查找,插入,删除
Linux 部署mysql 5.7全程跟踪 完整步骤 django部署
雪佛兰开拓者,安全保障温暖你的家庭出行的第一选择
从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
2D转换模块&&媒体查询
实验7(MPLS实验)
软考学习计划








