Talk about return and finally in Java

  • 2020-04-01 04:14:30
  • OfStack

In the past two days, we have learned the exception mechanism, in which try... The catch... Finally, I think it is an important link, connected with the sentence I learned before, I met the funny problem of return and finally. After the experiment, I found that the logical relationship of computer language is indeed delicate.

Let's look at the first piece of code


 public class return_finally{
   public static void main(String[] args){
     System.out.println( m_1() );
   }
   public static int m_1(){
     int i=10;
     try{
       System.out.println( "start" );
      return i+=10;
     }catch(Exception e){
       System.out.println( "error: "+e );
     }finally{
       if(i>10){
         System.out.println( i );
       }
       System.out.println( "finally" );
     }
     return i;
   }
 }

The output of the first code is as follows:
start
20
The finally
20
Notice that at this time, the second return is outside the finally statement. According to the stipulation that return and finally appear at the same time, we can understand that the first return is just a condition, and its function is just to find the finally statement. In fact, it just performs an operation of I +=10, and then directly enters the finally statement, and finally returns the result.
Let's look at the second piece of code:


 public class return_finally{
   public static void main(String[] args){
     System.out.println( m_1() );
   }
   public static int m_1(){
     int i=10;
     try{
       System.out.println( "start" );
      return i+=10;
      
     }catch(Exception e){
       System.out.println( "error: "+e );
     }finally{
       if(i>10){
         System.out.println( i );
       }
       System.out.println( "finally" );
       return 50; 
     }
   }
 }

The only difference between the second and the first is that the final return is placed in the finally statement, and we can guess the output:

start

20

The finally

50

At this point, the return has overwritten the previous result of the operation and returned the value of 50, which means that the return in the finally statement is executed

There's a third piece of code that you can enjoy:


public class return_finally{
  public static void main(String[] args){
    System.out.println( m_1() );
  }
  public static int m_1(){
    int i=10;
    try{
      System.out.println( "start" );
      return i;
    }catch(Exception e){
      System.out.println( "error: "+e );
    }finally{
      if(i>10){
        System.out.println( i );
      }
      System.out.println( "finally" );
        i=50; 
    }
    return i;
  }
}

I =50 more in the finally statement, so what is the result?

start

The finally

10

This is the result. There is no return in the finally statement, so the original return value does not change.

So can we understand the following from these three examples:

If there is a new algorithm in the statement, it will fetch the value from that space for operation. If there is a return in the finally, it will overwrite the "old value" of that space with the "new value" and finally return. If there is no return in finally, simply take the "old value" out of that space and return it back.

The above is purely understanding, I hope you give directions, thank you for your help!


Related articles: