Sample-final block cleansing

When I try to execute the following function in Java:

public static int myfunc (int x) {
    try {
        return x;
    } finally {
        x++;
    }       
}
public static void main (String args[]) {
    int y=5,z;
    z = myfunc(y);
    System.out.println(z);
}

The output printed on the console is 5, where, as you might expect, 6 will be printed. Any idea why?

+4
source share
5 answers

... would expect 6 to be printed. Any idea why?

x++occurs after the value has 5been read from x. Remember that what follows the statement returnis an expression. When an operator is reached return, this expression is evaluated (its value is determined), and then this resulting value is used as the return value of the function. So, myfunchere is the order of what happens:

  • Enter the tryblock.
  • x (, x).
  • .
  • finally .
  • x.
  • , 3.

, , x - 6, . x .

myfunc :

int y = x;
x++;

x ( , return x ), y, x. y . .

: foo "foo" 5 bar, "bar", :

int test() {
    try {
        return foo();
    }
    finally {
        bar();
    }
}

... foo, "foo", bar, "bar" test 5. , foo , (finally) ( ), : , return foo();, foo(). return x;: .

+5

finally , try. x x finally.

public static int myfunc (int x) {
    try {
        return x;  // returning
    } finally {
        x++;   // now incremented.
    }       
}

. , try-catch-finally, ( ) .

+4

, , , 5, , x.

package qwerty7;

public class Stack {
    public static int myfunc (int x) {
        try {
            return x;
        } finally {
            System.out.println("I'm executing!");
            x++;
        }       
    }
    public static void main (String args[]) {
        int y=5,z;
        z = myfunc(y);
        System.out.println(z);
    }
}

Output:
I'm executing!
5
0

:

public class ok {

public static int foo1(int x) {
    try {
        x++;
        System.out.println("foo1 : Inside try before returning I am " + x);
        return x;
    } finally {
        x++;
        System.out.println("foo1 : Inside Finally after post Increment I am " + x);
    }
}

public static int foo2(int x) {
    try {
        x++;
        System.out.println("foo2 : I am inside another method, incremented and changed to " + x);
        return x;
    } finally {

    }
}

public static void main(String[] args) {
    ok o = new ok();
    int val = 10; // The father of all

    int z = foo1(val);
    System.out.println("After executing foo1 I am " + z);

    int x = foo2(val);
    System.out.println("After executing foo2 I am " + x);

    System.out.println("Inside main() and un-altered I am : " + val);
}
}

return, try, finally. , , :

  • MadProgrammer: .
  • RishiKesh Pathak : return: immediately returns control to the method calling it. And finally: it will always be done. but will wait in line

Thanks, hope this helps.

PS: - In parallel, you should also consider accepting the answer here if you feel that one of them has helped closely or completely resolved your request

-1
source

The value xreturned from myfunc()before the increment in the block finally.

-2
source

Source: https://habr.com/ru/post/1598822/


All Articles