Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 522
Master Chief Avatar asked Dec 20 '25 20:12

Master Chief


1 Answers

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.

like image 165
Jens Avatar answered Dec 22 '25 10:12

Jens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!