Some Thoughts on Java finally

2010-08-05  来源:本站原创  分类:Java  人气:141 

A first look at the definition of Java fiannlly, Baidu came in:

finally usually try / catch statements used together to ensure that the regardless of whether an exception occurs, finally inside the code will be executed.

On this definition, I remember a face questions before the encounter, if the try statement in the return of the, finally will be the implementation of the code inside the What?

public static void main(String[] args) {
        try {
            System.out.println("try block");

            return;
        } finally {
            System.out.println("finally block");
        }
    }

Print results:
try block
finally block

What does this show? Even a return statement in the try, finally, the code will still be executed to.

Then I thought of another question, finally, the code is actually executed before the return, or return after the execution?

I designed this piece of code:

public class Test {

    public static void main(String[] args) {
        System.out.println(test());
    }

    public static String test() {
        try {
            System.out.println("try block");

            return test1();
        } finally {
            System.out.println("finally block");
        }
    }

    public static String test1() {
        System.out.println("return statement");

        return "after return";
    }
}

What is the first implementation of the return or the first implementation of the finally do?

The result is this:
try block
return statement
finally block
after return

We can see that the first call to the return of the methods before finally calling the code.

Produced a paradox here, since a method has return, why are there codes to be enforced?

We will look at the generated code:

public class Test {

  // Method descriptor #6 ()V
  // Stack: 1, Locals: 1
  public Test();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [8]
    4  return
      Line numbers:
        [pc: 0, line: 2]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: Test

  // Method descriptor #15 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 1
  public static void main(java.lang.String[] args);
     0  getstatic java.lang.System.out : java.io.PrintStream [16]
     3  invokestatic Test.test() : java.lang.String [22]
     6  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
     9  return
      Line numbers:
        [pc: 0, line: 5]
        [pc: 9, line: 6]
      Local variable table:
        [pc: 0, pc: 10] local: args index: 0 type: java.lang.String[]

  // Method descriptor #25 ()Ljava/lang/String;
  // Stack: 2, Locals: 2
  public static java.lang.String test();
     0  getstatic java.lang.System.out : java.io.PrintStream [16]
     3  ldc <String "try block"> [34]
     5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
     8  invokestatic Test.test1() : java.lang.String [36]
    11  astore_1
    12  getstatic java.lang.System.out : java.io.PrintStream [16]
    15  ldc <String "finally block"> [39]
    17  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
    20  aload_1
    21  areturn
    22  astore_0
    23  getstatic java.lang.System.out : java.io.PrintStream [16]
    26  ldc <String "finally block"> [39]
    28  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
    31  aload_0
    32  athrow
      Exception Table:
        [pc: 0, pc: 12] -> 22 when : any
      Line numbers:
        [pc: 0, line: 10]
        [pc: 8, line: 12]
        [pc: 12, line: 14]
        [pc: 20, line: 12]
        [pc: 22, line: 13]
        [pc: 23, line: 14]
        [pc: 31, line: 15]

  // Method descriptor #25 ()Ljava/lang/String;
  // Stack: 2, Locals: 0
  public static java.lang.String test1();
     0  getstatic java.lang.System.out : java.io.PrintStream [16]
     3  ldc <String "return statement"> [41]
     5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
     8  ldc <String "after return"> [43]
    10  areturn
      Line numbers:
        [pc: 0, line: 19]
        [pc: 8, line: 21]
}

Read this carefully:

0  getstatic java.lang.System.out : java.io.PrintStream [16]
     3  ldc <String "try block"> [34]
     5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
     8  invokestatic Test.test1() : java.lang.String [36]
    11  astore_1
    12  getstatic java.lang.System.out : java.io.PrintStream [16]
    15  ldc <String "finally block"> [39]
    17  invokevirtual java.io.PrintStream.println(java.lang.String) : void [26]
    20  aload_1
    21  areturn

In other words, compiled after the code into a first call test1, and store the return value, and then call the code inside finally, the last to return.

This code seems to be no practical significance, however, on the java program can still be helpful to understanding.

相关文章
  • Thoughts on java in the thread 2010-05-04

    Thoughts on java in the thread 1. The difference between processes and threads Simple language, it said that the implementation process is a process, and thread can be understood as the process of implementation of a program fragment. Man with a litt

  • Some Thoughts on Java finally 2010-08-05

    A first look at the definition of Java fiannlly, Baidu came in: finally usually try / catch statements used together to ensure that the regardless of whether an exception occurs, finally inside the code will be executed. On this definition, I remembe

  • Anonymous classes on Java, Some Thoughts on 2010-02-04

    2009-12-04 Anonymous classes on Java, Some Thoughts on Keywords: right java anonymous classes in Some Thoughts Introduction Java's anonymous classes are more interesting as a programming approach, in the swing on the registration of listeners, we oft

  • Java程序员[资源大全] 2015-01-09

    [编者按]Java几乎是许多程序员们的入门语言,并且也是世界上非常流行的编程语言.国外程序员Andreas Kull在其Github上整理了非常优秀的Java开发资源,推荐给大家.译文由ImportNew- 唐尤华翻译完成. 以下为具体资源列表. 构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化的方式进行配置,所以维护起来相当困难. Gradle

  • 推荐!国外程序员整理的Java资源大全 2015-01-13

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化的方式进行配置,所以维护起来相当困难. Gradle:Gradle采用增量构建.Gradle通过Groovy编程而不是传统的XML声明进行配置.Gradle可以很好地配合Maven进行依赖管理,并且把Ant脚本当作头等公民. 字节码操作 编程操作Java字节码的函数库. ASM:通用底层字节码操作及

  • 常用Java学习工具 2015-01-16

    ‍‍ 构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化的方式进行配置,所以维护起来相当困难. Gradle:Gradle采用增量构建.Gradle通过Groovy编程而不是传统的XML声明进行配置.Gradle可以很好地配合Maven进行依赖管理,并且把Ant脚本当作头等公民. 字节码操作 编程操作Java字节码的函数库. ASM:通用底层字节码

  • 构建应用程序的工具 2015-02-24

    构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化的方式进行配置,所以维护起来相当困难. Gradle:Gradle采用增量构建.Gradle通过Groovy编程而不是传统的XML声明进行配置.Gradle可以很好地配合Maven进行依赖管理,并且把Ant脚本当作头等公民. 字节码操作 编程操作Java字节码的函数库. ASM:通用底层字节码操作及

  • Java, equals () and == Thoughts (2) - heap and the stack 2009-07-01

    Java, equals () and == Thoughts (2) - heap and the stack 2009-02-09 13:53 First of all: in the java in the memory is divided into "stacks" and "heap" of these two (Stack and Heap). The basic data types is stored in the "stack"

  • Initial contact with the thoughts and feelings java 2010-11-29

    Prawns today was asked to write articles for my blog, think carefully, as a rookie new to Java programming in the near run, what really did not have something good to write a technical level, and put me in these days through contact with java, its Un

  • With regard to the use of java exceptions Some Thoughts 2010-02-13

    Has been unusual problems. To write about some of their own views. Many involve a number of commonly used java development framework. spring framework for all of the sql, or Hibernate Exception converted into their own unchecked exception (DataAccess

  • Java Capriccio: Scenario_1 (Java Random Thoughts, Part I) 2010-03-29

    Followings are my personal thought bubbles on java programming 1. Just don't put any instance variables into your Runnable 2. Be carefull when you're using 'valotile' 3. Use annotation as far as possible 4. If possible, anyway, use annotation to subs

  • Java language thoughts 2010-03-31

    We know that computer from the hardware point of view is used to process the data, and for some input, the output of the other side, the main data processing process, is the data exchange, in a single Zhuji On , an opening of the main yes Cunchu equi

  • Reading java core technology thoughts (1) 2010-07-24

    1.java class naming camel naming the class name is capitalized letter at the beginning of the term. If the name of multiple words, each word should be capitalized the first letter of its own, for example, should be written in CamelCase 2.condition? e

  • Thoughts on the Java programming advice 2010-09-20

    Java programs written notes, advice on Java programming ideas. (1) the class name the first word should be capitalized. Fields, methods, and object (handle) the first letter should be lowercase. For all identifiers, which contains all of the words sh

  • Java Capriccio: Scenario_2 (Java Random Thoughts, Part II) 2010-03-29

    1.Create & Destroy an object a.public static Type valueOf (String name); b.factory method: Type createByName (String name); These methods can return subclass type [May return to sub-type, while the Constructor will not work] Do never use 'new' to cre

  • java through point to point communications within the network to achieve Thoughts 2010-03-18

    1, the client A landing time, to send a UDP packets to the server, and then the server under the packets to obtain the client IP and port A, and save it to a UdpAddressMap (with the login name as the key to save) in. 2, the client to perform operatio

  • How to use the Java 5 Annotation (Notes) to simplify the Hibernate code 2009-07-19

    Note how to use Java 5 to simplify the Hibernate code, persistence layer and the coding process easier. Traditionally, Hibernate configuration relies on external XML documents: a database mapping is defined as a set of XML mapping file, and loaded at

  • java using a proxy server to access the network 2010-03-29

    This technique will show you on how to write access to the Internet through a proxy Web server Java applications. In the Java applications by adding an extra agent support just write a few lines of code, and does not rely on any security "loopholes.&

  • Java programmers must-read ten books programming skills (change) 2010-04-09

    Kevin Taylor of the Java books recommended books on the concurrent bookstores like I have not seen before. In the Web providing the only Java Servlet Programming debut, JSP, etc., and not mentioned it. Development tools, there is no one, it is I am a

  • [Change] Java NIO with the use of the original li 2010-03-23

    Java NIO non-blocking application of generally applicable to be used in I / O read and write and so on, we know that system is running a performance bottleneck typically I / O read and write, including the port and the file operation, the past, to op