Search code examples
javaeclipsedebuggingshort-circuiting

Different result during Debug and Run in Java


I wrote this small piece of code to test Short-circuit operator

package com.MasterChief;

public class ShortCircuitDebugTest {
    static boolean myBool = true;
    static int myInt = 1;
    public static void main(String[] args) {
        if (myBool || myFunc()){ // line 7: put break point here
            System.out.println("myInt = " + myInt);
        }
    }

    private static boolean myFunc(){
        myInt = 5;
        return !myBool;
    }
}

I put a Breakpoint at line 7 and added myFunc in the Expressions window. During debug Expressions window should be visible. Result is:

myInt = 5

If I just run the program or do not add myFunc in Expressions window or do not select Expressions window, I do not get this behaviour.
Is this is an expected behaviour or a bug in Eclipse?


Solution

  • If you add the expression to the expression Window it will executed and change the value of the global variables myBooland myInt. If you go to the next step it will executed a second time and so your code will give you an other result.