# 1-第一季-01_尚硅谷_JavaSE面试题:自增变量-2021-03-04
# ❤ 面试题
![](https://cdn.nlark.com/yuque/0/2021/jpg/621440/1614832202701-df5e759e-2588-4f69-b757-f1ecfda3fcab.jpg#align=left&display=inline&height=540&margin=%5Bobject%20Object%5D&originHeight=540&originWidth=960&status=done&style=none&width=960)
## 1、自测结果(错误!)
i = 1 --> 2 --> 4
j = 3
k = 4 + 3*3 = 13
### 1.1、自测结果(知道 i 值后的自测)(正确!)
int i = **1 --> 2 --> 1** --> 2 --> 3 --> 4
int j = 1 (重点:需要注意 i++ 为用后加)
int k = 2 + 3*3 = 11
## 2、运行结果
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832202790-078850c4-1daa-41d1-9c05-6ee5c4d9b669.png#align=left&display=inline&height=342&margin=%5Bobject%20Object%5D&originHeight=342&originWidth=935&status=done&style=none&width=935)
# 一、面试题解析:i = i++
## 1、 执行过程
- ① 把 i 的值压入操作数栈
- ② i 变量自增1**(重点:自增1是在局部变量表中进行的!)**
- ③ 把操作数栈中的值赋值给 i**(重点:赋值过程为操作数栈的值覆盖局部变量表中的值的过程???!!!)**
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832202896-d69bbf87-9ca6-4b47-89d4-58563359be43.png#align=left&display=inline&height=171&margin=%5Bobject%20Object%5D&originHeight=171&originWidth=652&status=done&style=none&width=652)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832202964-f147fbc3-48ef-4c3d-aaf1-f877f61669b5.png#align=left&display=inline&height=192&margin=%5Bobject%20Object%5D&originHeight=192&originWidth=650&status=done&style=none&width=650)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203057-5bfa0065-1f45-43c5-ba81-be573b10ae7d.png#align=left&display=inline&height=204&margin=%5Bobject%20Object%5D&originHeight=204&originWidth=652&status=done&style=none&width=652)
## 2、字节码角度分析
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203114-f44adee1-47b5-402d-ae65-74b735f81212.png#align=left&display=inline&height=413&margin=%5Bobject%20Object%5D&originHeight=413&originWidth=897&status=done&style=none&width=897)
## 3、细节说明:i = i++
- 同时在**局部变量表**和**操作数栈**中存在 i 。
- i++ 是在**局部变量表**中执行的!!!!
- 赋值操作会使得操作数栈的 i 值,**覆盖**局部变量表中的 i 值。(关键点:覆盖)
- 另外注意:i ++ 是用后加
# 二、面试题解析:int j = i++
## 1、 执行过程
- ① 把 i 的值压入操作数栈
- ② i 变量自增1
- ③ 把操作数栈中的值赋值给 j
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203218-f9aea015-8f82-44af-9cb1-848d1d8318ed.png#align=left&display=inline&height=282&margin=%5Bobject%20Object%5D&originHeight=282&originWidth=661&status=done&style=none&width=661)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203278-b8ba4332-680c-4e6e-b0ae-47aaaaca870b.png#align=left&display=inline&height=270&margin=%5Bobject%20Object%5D&originHeight=270&originWidth=661&status=done&style=none&width=661)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203343-1046f887-5384-4453-95a1-2be2e8a5a1ec.png#align=left&display=inline&height=274&margin=%5Bobject%20Object%5D&originHeight=274&originWidth=662&status=done&style=none&width=662)
## 2、字节码角度分析
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203412-4f4a9c62-b947-4736-aa7f-5cad71233844.png#align=left&display=inline&height=343&margin=%5Bobject%20Object%5D&originHeight=343&originWidth=895&status=done&style=none&width=895)
# 三、面试题解析:int k = i + ++i * i++
## 1、 执行过程
- ① 把 i 的值压入操作数栈
- ② i 变量自增1
- ③ 把 i 的值压入操作数栈
- ④ 把 i 的值压入操作数栈
- ⑤ i 变量自增
- ⑥ 把操作数栈中前两个弹出求乘积结果再压入栈
- ⑦ 把操作数栈中的值弹出求和再赋值给 k
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203484-978f4dd3-b3a3-47b8-95b6-525987087a58.png#align=left&display=inline&height=269&margin=%5Bobject%20Object%5D&originHeight=269&originWidth=666&status=done&style=none&width=666)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203551-9ec3827b-9e3d-4b2a-a6ab-e5070df03d9f.png#align=left&display=inline&height=257&margin=%5Bobject%20Object%5D&originHeight=257&originWidth=663&status=done&style=none&width=663)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203612-91c470ed-c4cd-4112-ae8f-471e09991eb9.png#align=left&display=inline&height=262&margin=%5Bobject%20Object%5D&originHeight=262&originWidth=663&status=done&style=none&width=663)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203687-2dd7f017-3aef-4cdc-8991-967e1683930c.png#align=left&display=inline&height=260&margin=%5Bobject%20Object%5D&originHeight=260&originWidth=665&status=done&style=none&width=665)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203777-70d646e5-6c1b-4fed-86e4-1b381ec337a0.png#align=left&display=inline&height=269&margin=%5Bobject%20Object%5D&originHeight=269&originWidth=663&status=done&style=none&width=663)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203848-8b2c6864-3add-40c1-8599-258b36ffb7ae.png#align=left&display=inline&height=269&margin=%5Bobject%20Object%5D&originHeight=269&originWidth=666&status=done&style=none&width=666)
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203907-645d758c-edf2-4e21-9eba-9727d8fdc8c5.png#align=left&display=inline&height=255&margin=%5Bobject%20Object%5D&originHeight=255&originWidth=662&status=done&style=none&width=662)
## 2、字节码角度分析
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832203963-92c81b84-3854-4859-8d4d-b8eb5a3a50f9.png#align=left&display=inline&height=455&margin=%5Bobject%20Object%5D&originHeight=455&originWidth=891&status=done&style=none&width=891)
# 四、小结
![](https://cdn.nlark.com/yuque/0/2021/png/621440/1614832204058-91d1d6eb-3d74-4093-be8a-1f183ed25923.png#align=left&display=inline&height=280&margin=%5Bobject%20Object%5D&originHeight=280&originWidth=903&status=done&style=none&width=903)
---
# ☀ 课堂小测
- 回答上述面试题。
- **i = i++ ** 字节码和执行过程?细节说明?
- **int j = i++** 字节码和执行过程?
- **int k = i + ++i * i++** 字节码和执行过程?
- 自增变量的运算小结?
网友评论