Java returned value is defined as a method of type int return 1 return int is the return or Integer && finally issues

 

 

Back in Java int type value is defined as a method return 1; Integer value is returned, when the return value of type 1 is substantially encapsulated type Integer.

Define a Test class, and exception handling try finally respectively return;

 

public class Test {

    public static void main(String[] args) {
         System.out.println(new Test().test());
     }
     int test() {
         try {
             return func1();
         }finally{
             return func2();
         }
     }

     int func1() {
         System.out.println("func1");
         return 1;
     }

     int func2() {
         System.out.println("func2");
         return 2;
     }

 

operation result:

 

Ah, no problem, the final performance finally change the return value of return. In a look at the following:

 

public class Test {

    public static void main(String[] args) {
         System.out.println(new Test().test());
     }
    static int test()  {
        int x = 1;
        try {
            return x;
        }finally {
            ++x;
        }
    }
}

 

 

 According to the above logic will return the execution result is 2, but the result is not the case:

 

Here we must finally talked about the process for the return of:

Finally block process the return value, when defined as a reference type, will try to return a return value, and then reset the finally return value, when defining basic types, try the return value is not reset.

Abnormal code with a try statement it will mark the Throwable thread monitor runtime operation of the method, when an exception occurs, referred abnormal logic, method of operation, the stack memory, will be "out of the advanced" in accordance with the principle of execution, main method calls abnormal methods, main method in the underlying abnormality in the upper method.

When the basic type, abnormal method is performed after the completion of return x, the return value of this method to determine a fixed value (base value of type copy), then finally block modification has no meaning (the similarity value is transmitted, can be finally block See method do body) when the reference type, because the address is copied, the value will change. (Similar to the address transfer)

 

That is, when defined as an int type, return x; value is copied, it finally can not modify the value of x.

As for the return 1; when, as a result of changes occurs, i.e., returns the value of the finally, is the address of the copy, return type is a reference, to determine the return 1; in this case, return new Integer (1);

I.e. return 1; Integer object is returned.

About the return of treatment finally; learning and "write quality code (Java program to improve the 151 recommended)" Article 113 of the book is recommended ---- Do not handle the return value in a finally block; notes see blog: https: / /blog.csdn.net/sanhewuyang/article/details/84333062

Inadequate understanding of the please correct me. Ah, we would have been tenderness pending. ^ _ ^

 

Guess you like

Origin www.cnblogs.com/liruilong/p/11250521.html