当前位置:网站首页>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;
边栏推荐
- 2022 China Logistics Industry Conference and Entrepreneur Summit Forum will be held in Hangzhou!
- Word文件损坏如何修复
- flutter 做底部的三个按键,有叠加,有填充
- What are the efficient open source artifacts of VSCode
- [Meng Xin problem solving] Delete the Nth node from the bottom of the linked list
- Unity 加载读取PPT
- Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。
- 雪佛兰开拓者,安全保障温暖你的家庭出行的第一选择
- 【VisDrone数据集】YOLOV4训练VisDrone数据集步骤与结果
- 【深入浅出玩转FPGA学习14----------测试用例设计2】
猜你喜欢
随机推荐
Game mall table establishment
The performance management method OKR is used by all companies
pytorch双线性插值
Jetpack Compose学习(8)——State及remeber
ctfshow 文件包含
Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
数据清洗-使用es的ingest
2D Transform Module && Media Queries
第一个独立完成的千万级项目
寄存器(汇编语言)
【飞控开发基础教程10】疯壳·开源编队无人机-PID 基础原理
软考学习计划
xss绕过:prompt(1)
Shell programming conditional statement test command Integer value, string comparison Logical test File test
xss靶机训练【实现弹窗即成功】
The first level must project independently
How to open the payment channel interface?
HCIP第十五天笔记
How to solve the error of joiplay simulator
(五)fastai应用