Skip to content

Commit 3f23615

Browse files
committed
Update Java Notes
1 parent 6677043 commit 3f23615

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

Java.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13096,7 +13096,7 @@ public static void main(String[] args) {
1309613096

1309713097
```java
1309813098
0: iconst_0
13099-
1: istore_1 // 0 -> i
13099+
1: istore_1 // 0 -> i ->赋值
1310013100
2: bipush 10 // try 10 放入操作数栈顶
1310113101
4: istore_1 // 10 -> i 将操作数栈顶数据弹出,存入局部变量表的 slot1
1310213102
5: bipush 30 // finally
@@ -13131,6 +13131,8 @@ LocalVariableTable:
1313113131

1313213132
##### return
1313313133

13134+
###### 吞异常
13135+
1313413136
```java
1313513137
public static int test() {
1313613138
try {
@@ -13142,21 +13144,24 @@ public static int test() {
1314213144
```
1314313145

1314413146
```java
13145-
0: bipush 10 // <- 10 放入栈顶
13146-
2: istore_0 // 10 -> slot 0 (从栈顶移除了)
13147-
3: bipush 20 // <- 20 放入栈顶
13148-
5: ireturn // 返回栈顶 int(20)
13149-
6: astore_1 // catch any -> slot 1
13150-
7: bipush 20 // <- 20 放入栈顶
13151-
9: ireturn // 返回栈顶 int(20)
13147+
0: bipush 10 // 10 放入栈顶
13148+
2: istore_0 // 10 -> slot 0 (从栈顶移除了)
13149+
3: bipush 20 // 20 放入栈顶
13150+
5: ireturn // 返回栈顶 int(20)
13151+
6: astore_1 // catch any -> slot 1 存入局部变量表的 slot1
13152+
7: bipush 20 // 20 放入栈顶
13153+
9: ireturn // 返回栈顶 int(20)
13154+
Exception table:
13155+
from to target type
13156+
0 3 6 any
1315213157
```
1315313158

1315413159
* 由于 finally 中的 ireturn 被插入了所有可能的流程,因此返回结果肯 finally 的为准
13155-
* 跟上例中的 finally 相比,发现没有 athrow ,这告诉我们:如果在 finally 中出现了 return,会**吞掉异常**
13160+
* 字节码中没有 **athrow** ,表明:如果在 finally 中出现了 return,会**吞掉异常**
1315613161

1315713162

1315813163

13159-
##### 面试题
13164+
###### 不吞异常
1316013165

1316113166
```java
1316213167
public class Demo3_12_2 {
@@ -13167,7 +13172,7 @@ public class Demo3_12_2 {
1316713172
public static int test() {
1316813173
int i = 10;
1316913174
try {
13170-
return i;
13175+
return i;//返回10
1317113176
} finally {
1317213177
i = 20;
1317313178
}
@@ -13176,19 +13181,22 @@ public class Demo3_12_2 {
1317613181
```
1317713182

1317813183
```java
13179-
0: bipush 10 // <- 10 放入栈顶
13180-
2: istore_0 // 10 -> i
13181-
3: iload_0 // <- i(10)
13182-
4: istore_1 // 10 -> slot 1,暂存至 slot 1,目的是为了固定返回值
13183-
5: bipush 20 // <- 20 放入栈顶
13184-
7: istore_0 // 20 -> i
13185-
8: iload_1 // <- slot 1(10) 载入 slot 1 暂存的值
13186-
9: ireturn // 返回栈顶的 int(10)
13187-
10: astore_2
13188-
11: bipush 20
13189-
13: istore_0
13190-
14: aload_2
13191-
15: athrow
13184+
0: bipush 10 // 10 放入栈顶
13185+
2: istore_0 // 10 -> i,赋值给i,放入slot 0
13186+
3: iload_0 // i(10)加载至操作数栈
13187+
4: istore_1 // 10 -> slot 1,暂存至 slot 1,目的是为了固定返回值
13188+
5: bipush 20 // 20 放入栈顶
13189+
7: istore_0 // 20 -> i
13190+
8: iload_1 // slot 1(10) 载入 slot 1 暂存的值
13191+
9: ireturn // 返回栈顶的 int(10)
13192+
10: astore_2
13193+
11: bipush 20
13194+
13: istore_0
13195+
14: aload_2
13196+
15: athrow // 不会吞掉异常
13197+
Exception table:
13198+
from to target type
13199+
3 5 10 any
1319213200
```
1319313201

1319413202

0 commit comments

Comments
 (0)