JavaSE get to know the complete implementation of try, catch, finally with the return of

Disclaimer: This article is CSDN blogger original article, "Garnett Shin Ki", follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_36262896/article/details/79779953


 Before starting my blog, I will quote Mashi Bing teacher's words, master the memory you mastered all (funny).

         In general, try to execute the program inside the content will finally perform content inside. If you encounter a return in the try or catch it, then the function after the completion of the implementation of the expression behind the return, which will go to the finally block of statements before the end of the method body. But there is a special case, if there is System.exit (0) in the try which such statement, System.exit (0) is the termination of the Java Virtual Machine JVM, JVM even have stopped, all over, of course, also finally statement It will not be executed. Now consider the general case, try, catch, finally with the return of execution.

(A) to discuss the implementation process of the return of memory allocation (my personal opinion)
        return marks the end of the current method is equivalent to a body. It's a bit like break out of the cycle. If the current body is a method that returns a value, then the current return to the first method is performed, allocates a memory space in the stack, for the return type of the function type, return the value of the calculated value after an expression.

        If the second execution to return (such as the inside of the finally return back inside the return try), then the piece of the stack space allocated memory contents will be replaced with the new calculated value of the expression return later.

        So when the end of the current method with a type thereof, will leave the stack in the return value. With or without the return value is assigned to the other variables, this space will be reclaimed.

(B) returns the value of the type discussed

1. The return value is the basic data types.

        In this case, the data returned will not be affected by the statement other than return.

example:

        

        In fact, well explained by the memory views, return i; statement in the stack is actually opened up a new space, and then the value of i is assigned to the new space opened up, so after the assignment is completed, the value of i is how change does not affect the return value.

2. The return value is a reference data type.

        In this case, the data returned may be affected by the statement outside return.

example:

    

        Also good to explain memory point of view, return i; statements will open up a new space, the value of i is assigned to the newly opened new space opened up. But i is a reference type, if the operation of the object i points, then also affect the return value, because they point to the same memory space.

Summary: In fact summed up on two points

1: a function to execute content try inside, will be inside the finally statement block, unless there are similar try System.exit () method.

2: the function executes a return may not return directly (such try and catch the inside), but this method will open a return type of body space, the value of which is stored behind the return result of an expression, which results It is covered by a second return (finally inside).

 

Published 113 original articles · won praise 25 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_42006733/article/details/104824913