`
elan1986
  • 浏览: 164849 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Finally in Java

阅读更多
我们从几个问题开始:
一、finally一定会执行吗?
二、下面语句的执行结果是:
public class FinallyTest {
         public static void main(String[] args) {
                   System.out.println("getValue()返回值为:" + getValue());
         }
        
         public static int getValue() {
                   try {
                            return 0;
                   } finally {
                            return 1;
                   }
         }
}

三、下面语句的执行结果是:
public class FinallyTest {
         public static void main(String[] args) {
                   System.out.println("getValue()返回值为:" + getValue());
         }
        
         public static int getValue() {
                   int i = 1;
                   try {
                            return i;
                   } finally {
                            i++;
                   }
         }
}

首先,第一个问题,答案是否定的。在try里面执行了System.exit(0);操作finally就不会执行,另外死机、断电都会导致finally不会执行。
对于后面两个问题,我们先看一下摘自《THE Java™ Programming Language, Fourth Edition》By Ken Arnold, James Gosling, David Holmes中的几段话:
a finally clause is always entered with a reason. That reason may be that the try code finished normally, that it executed a control flow statement such as return, or that an exception was thrown in code executed in the TRy block. The reason is remembered when the finally clause exits by falling out the bottom. However, if the finally block creates its own reason to leave by executing a control flow statement (such as break or return) or by throwing an exception, that reason supersedes the original one, and the original reason is forgotten. For example, consider the following code:
try {
    // ... do something ...
    return 1;
} finally {
    return 2;
}
When the TRy block executes its return, the finally block is entered with the "reason" of returning the value 1. However, inside the finally block the value 2 is returned, so the initial intention is forgotten. In fact, if any of the other code in the try block had thrown an exception, the result would still be to return 2. If the finally block did not return a value but simply fell out the bottom, the "return the value 1" reason would be remembered and carried out.
从红色和蓝色字体标注的解释可以看出,当finally中有return语句的时候,try中的return会被抛弃;当finally中没有return语句时,try中return的值会被保存且被返回。
所以,第二个问题的答案是:getValue()返回值为:1
第三个问题的答案是:getValue()返回值为:1
分享到:
评论

相关推荐

    Concurrent, Real-Time and Distributed Programming in Java: Threads, RTSJ and RMI

    Finally, a presentation of programming distributed in Java is presented in this book. We are particularly interested in communication using the TCP Sockets and high-level communication using Java ...

    Thinking in Java简体中文(全)

    2. Java的学习 3. 目标 4. 联机文档 5. 章节 6. 练习 7. 多媒体CD-ROM 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 14. 致谢 第1章 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 ...

    Manning Java Reflection In Action

    Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time providing the sound conceptual basis that developers need to ...

    Java Reflection in Action

    Java Reflection in Action is unique in presenting a clear account of all the cool things you can do with reflection, and at the same time pro- viding the sound conceptual basis that developers need to...

    Thinking in Java 中文第四版+习题答案

    2. Java的学习 3. 目标 4. 联机文档 5. 章节 6. 练习 7. 多媒体 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 14. 致谢 第1章 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 实现方案的...

    JAVA_Thinking in Java

    2. Java的学习 3. 目标 4. 联机文档 5. 章节 6. 练习 7. 多媒体CD-ROM 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 14. 致谢 第1章 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 ...

    Cloud-Native+Applications+in+Java-Packt+Publishing(2018).epub

    You will learn about design patterns specific to applications running in the cloud and find out how you can build a microservice in Java Spring using REST APIs. You will then take a deep dive into ...

    The Definitive Guide to JSF in Java EE 8

    turbulent announcement by Oracle in 2017 that Java EE, thus including JSF, would be transferred to the Eclipse foundation. Oracle would stop leading the specs it owned before, which again includes ...

    Java Language Features 2nd Edition

    My first encounter with the Java programming language was during a one-week Java training session in 1997. I did not get a chance to use Java in a project until 1999. I read two Java books and took a ...

    JavaServer Faces in Action

    As I write this foreword, I am collaborating with four leading user interface (UI) component vendors on ...loud and clear: they are very relieved to finally have a standard for web-based user interfaces.

    JAVA_Thinking in Java(中文版 由yyc,spirit整理).chm

    JAVA_Thinking in Java(中文版 由yyc,spirit整理).chm ------------------------------------------------- 本教程由yyc,spirit整理 ------------------------------------------------- “Thinking in Java...

    2018 Scala for Java Developers: A Practical Primer

    a tour of Scala, a comparison between Java and Scala, Scala-specific features and functional programming idioms, and finally a discussion about adopting Scala in existing Java teams and legacy ...

    java实验7.1(CollegeCourse.java)

    Finally, write a program that uses the Auto class and subclasses to display information about different cars. The java programs should be named like: Auto.java Ford.java Chevy.java UseAuto.java ...

    The Java Module System.epub

    Java's much-awaited "Project Jigsaw" is finally here! Java 11 includes a built-in modularity framework, and The Java Module System is your guide to discovering it. In this new book, you'll learn how ...

    Mastering Java for Data Science

    Finally, we finish the book by talking about the ways to deploy the model and evaluate it in production settings. What you will learn Get a solid understanding of the data processing toolbox ...

    Apress.Beginning.Java.8.Fundamentals

    My first encounter with the Java programming language was during a one-week Java training session in... Finally, it was always fun to play with Java programs, sometimes for hours, to add them to the book

    Think in Java(中文版)chm格式

    2. Java的学习 3. 目标 4. 联机文档 5. 章节 6. 练习 7. 多媒体CD-ROM 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 14. 致谢 第1章 对象入门 1.1 抽象的进步 1.2 ...

    Thinking in Java(中文版 由yyc,spirit整理).chm

    “Thinking in Java”详细目录   写在前面的话 引言 1. 前提 2. Java的学习 3. 目标 4. 联机文档 5. 章节 6. 练习 7. 多媒体CD-ROM 8. 源代码 9. 编码样式 10. Java版本 11. 课程和培训 12. 错误 13. 封面设计 ...

    Mastering Concurrency Programming with Java 8

    Finally, the book ends with a detailed description of the tools and techniques that you can use to test a Java concurrent application., Style and approach, A complete guide implementing real-world ...

    Hands-On Enterprise Java Microservices with Eclipse MicroProfile

    Finally, you’ll be guided in developing a conference application using Eclipse MicroProfile, and explore possible scenarios of what’s next in MicroProfile with Jakarta EE. By the end of this book,...

Global site tag (gtag.js) - Google Analytics