Showing posts with label finally. Show all posts
Showing posts with label finally. Show all posts

Tuesday, 27 May 2014

Why the return value is 0 instead of 100?

public class FinallyReturnVariableAlter
{
   public static void main(String[] args)
  {
     System.out.println(test());
  }

  public static int test()
 {
    int i=0;
    try
   {
      return i;
   }
   finally
  {
    i+=100;
  }
  }
}

 Answer By Lokesh Gupta :
  • Every function call in java leads to creation of extra memory space created where return value is stored for reference after function execution completes and execution return to caller function back.
  • At that time, that special memory address where return value is stored, if referenced back as return value of function.
  • Here, trick is, the memory slot store the value of returned value, not its reference. So, once any return statement is encountered its value is copied in memory slot. That’s why if you change the value of “i” in finally, return value does not change because its already copied to memory slot.
  • We any other return statement is encountered (e.g. in finally block), its value will be overwritten again. So, it you want to return 100, use return statement in finally also.;

Thursday, 22 May 2014

Java's return value in try-catch-finally mechanism

Hello friends again question with try,catch and finally..i like it ...lol..

Here sample code is :

public class J2 extends J1 {

    public static int function() {
        try {
            return 1; // We think after return function complete here ...?
        } catch (Exception e) {
            return 2;
        } finally {
            return 3;      // But actually  this block execute successfully
    }
 }

    public static void main(String[] args) {
        System.out.println(function());
    }
}


I have no idea how the JVM implements it, but the simplest way to look at it (at least conceptually) would be:
  1. the return value in the "try" is pushed onto the stack,
  2. then the "finally" block is executed,
  3. the new return value is pushed onto the stack
  4. the function exits, and the return value is popped from the stack, thus ignoring the first one.


What I found in the Java language specification at least defines that your code snippet should return 3. Of course, it does not mention how the JVM should implement this, and what possible optimizations one could do.
Section 14.20.2 defines that
If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
  1. If the finally block completes normally, then the try statement completes abruptly for reason R.
  2. If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).
And the start of chapter14 (section 14.1 to be more precise) specifies what a normal and abrupt completion is. For example a return with a given value is an abrupt completion.
Hence in this case, the finally block completes abruptly (reason: return with a given value), so the try will complete abruptly for the same reason (and return 3). This is confirmed in section 14.17 about the return statement as well
If evaluation of the Expression completes normally, producing a value V, then the return statement completes abruptly, the reason being a return with value V.



About finally block ...


Hello friends my Today's question is :

Here is sample code :

        File file = new File("d:/asd.txt"); //Assume this path is not available
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) { // So Exception catch Here
                       e.printStackTrace();
        }
  // So now finally block must execute 
     finally {
            try {
                fileInputStream.close();  // but here again exception comes here
            } catch (IOException e) {
                e.printStackTrace();   // So Exception catch Here
            }finally {
                System.out.println("inner finally");  // This statement executed
            }
            System.out.println("finally"); // Why this statement is not execute .. ?
        }
Provide your Comments and feedback ...Thanks is advance..!!!!