Talking about return statement _java in try-catch-finally in Java

Source: Internet
Author: User

We know that the return statement is used in a method, the first is used to return the execution result of a function, and the second is to return a function that is of type void, and is simply a returns statement (return;), which is used to end the execution of the method, which means that the statement will not be executed. Of course, in this case, there is no other statement after the return statement.

There are some queries using the return statement in the try-catch-finally statement

Code One:

static int Intc () {
int x =0;
try{
x=1;
return x;
} finally {
x = 3; 
}
}

Code two: Add a return statement to the finally statement in the above code

static int Intc () {
int x =0;
try{
x=1;
return x;
} finally {
x = 3;
return x;
}

Code Three:

static int Intc () {
int x =0;
try{
x=1;
return x;
} finally {
x = 3;
return 0;
}

So what are the results of these three methods?

Code one: return 1;
Code two: return 3;
Code three: return 0;

What is the principle of it?

The reason is that when a Java virtual machine executes a method that has a return value, it creates a region in the local variables list to store the method's return value, which is returned from the region when the return statement is executed.

In code one, 1 is assigned to the variable x in a try, and then the value of the variable x is copied to the region where the return value is stored, and the last return value range is 1, and one is returned when the return statement is executed.

And in code two, 1 is also assigned to the variable x, the value of x is then copied to the region where the return value is stored, at which point the value of the returned value is 1, and then jumps to the finally statement, where 3 is assigned to the local variable x, and then the value of x is copied to the region where the return value is stored, and the return statement is The value in the returned range read to is 3.

In code three, the statements executed in try are the same, jump to the finally statement, assign 3 to the local variable, then assign 0 to the region where the return value is stored, and the return statement, the value in the returned region read to 0, so return 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.