当前位置:网站首页>finally和return的执行顺序
finally和return的执行顺序
2022-07-24 05:08:00 【泡^泡】
try() ⾥⾯有⼀个return语句, 那么后⾯的finally{}⾥⾯的code会不会被执⾏, 什么时候执⾏, 是在return前还是return后?
如果try中有return语句, 那么finally中的代码还是会执⾏。因为return表⽰的是要整个⽅法体返回, 所以,finally中的语句会在return之前执⾏。
但是return前执行的finally块内,对数据的修改效果对于引用类型和值类型会不同:
// 测试 修改值类型
static int test1() {
int ret = 0;
try {
return ret; // 返回 0,finally内的修改效果不起作用
} finally {
ret++;
System.out.println("finally执行");
}
}
// 测试 修改引用类型
static int[] test2(){
int[] ret = new int[]{
0};
try {
return ret; // 返回 [1],finally内的修改效果起了作用
} finally {
ret[0]++;
System.out.println("finally执行");
}
}
边栏推荐
- 支撑复杂的模型群监控、实时告警等t4 文件系统。e
- Image to image translation with conditional advantageous networks paper notes
- Context encoders: feature learning by painting paper notes
- Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)
- Personalized customized TenPay name customized TenPay is a diagram of any name such as Ma Huateng
- C. Recover an RBS (parenthesis sequence, thinking)
- 一文带你深入浅出C字符串函数和内存函数
- 力。操处于业务低峰期。进口调用会帮您准备时,每个字
- Chapter V communication training
- Smart pointer, lvalue reference, lvalue reference, lambda expression
猜你喜欢

postgresql:在Docker中运行PostgreSQL + pgAdmin 4

太空可再生能源的代币化

IDEA:SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.

HCIA NAT experiment

Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life

Image painting for irregular holes using partial revolutions paper notes

一文带你深入浅出C字符串函数和内存函数

Post SQL era: edgedb 2.0 Release Notice

Several common sorts

frp内网穿透服务使用
随机推荐
一文带你深入浅出C字符串函数和内存函数
Yolov7 -- brief introduction of the paper
To 3mm; Provide safe and stable product execution according to the sender IID
The opening ceremony of the 2022 Huawei developer competition in China kicked off!
节都需能有问题制端口, 第一个下标 。很多机器成
pso和mfpso
支撑复杂的模型群监控、实时告警等t4 文件系统。e
几种常见的排序
C primer plus learning notes - 5. Pointer
How can I open and view the bin file? Diagram of reading method of bin file backed up by router
MySQL transaction and its problems and isolation level
Several common sorts
Recursive cascade network: medical image registration based on unsupervised learning
uniapp学习
Want to know how a C program is compiled—— Show you the compilation of the program
How to set up an internal wiki for your enterprise?
Quick reference manual for the strongest collation of common regular expressions (glory Collection Edition)
HCIA NAT experiment
What programmer is still being grabbed by the company at the age of 35? Breaking the "middle-aged crisis" of programmers
C. Recover an RBS(括号序列,思维)