3

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

This is not a bug. Since || operator evaluates expressions from left to right your myBool check will return true and the myFunc() will NOT get called (since the entire condition will always be true there is no need to evaluate myFunc() result).

Since you use the myFunc() in the debug expressions window it will be evaluated nonetheless to show you the result and will change the myInt to 5.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.