简体   繁体   中英

Inspect function of eclipse tricks me like a dummy, why does it seem to be that tricky while debugging?

Eclipse is awesome for writing java programs but today I find that it's awesome to trick new coders like me. @_@

I write a snippet as following,

public static void main(String[] args) {
    for(int i=0;i<3;i++){
        System.out.println("i = " + i);
    }
}

then I add a breakpoint at the line of "System.out.print....", click "Debug" button, eclipse goes to the "Debug" perspective and the line of breakpoint is highlighted, then I move the cursor over variable "i", its value is "0" as expected.

And then, I select the "i++" and click "Inspect"(or press "Ctrl+Shift+I") once, I move the cursor over variable "i", its value changed to "1". I repeat "Inspect" again, the value of i changed to "2"......(its value will add by 1 every time I clicked the "Inspect")!!

Why does this happend!? I ONLY want to watch the value of "i" for debug propuse, DO NOT want to really change its value until I step into next statement. I think that "Inspect", as well as "Display" are only for viewing the variable/expression, they should not impact the value, but in this case, it doesn't work as I expect to.

Could anyone tell me what went wrong?

My eclipse version info:
Version: Indigo Service Release 2
Build id: 20120216-1857

选择“ i ++”,然后单击“检查”

If you inspect an expression, eclipse has to execute that expression so you can get the value. Therefore, if you inspect i++ , eclipse adds one to i .

Think about it this way: If instead of i++, you inspected myFunction(i) , would you expect eclipse to execute the function "myFunction" to get the value? It's the same with i++ .

In eclipse if you inspect an expression then eclipse execute that expression and gives you result .
So in your case if you inspect i++ then eclipse execute it and increment the value of i . As many time as you run this this will increment value of i .

If you are concerned about displaying/showing values while debugging and do you want to be sure not affecting the value, you should select the variable or expression and use the "Watch" option.

This will track the variable/expression value without executing, just updating the new value each time this is run. I think is the most secure way.

As Pablo mentioned, it has to evaluate the code in order to tell you what value it returns. You could instead put a watch on "i+1" and that would give you the value you want without the side effect you don't want.

Basically, you need to be aware of any side effects of anything you launch, whether from the "main" code or from the debugging tools. As Erhannis mentioned, this is very useful at times for modifying values while debugging your code. (For example, you can verify that a tweak/fix is indeed helpful before actually tweaking your code.)

You were expecting "an isolate area" but this would be extremely hard to do, especially in an object-oriented context where many objects are linked to many other objects. Running the whole thing in parallel might sometimes work, but you'd lose the tweaking ability above. And in any context, you'd run into tons of problems with fighting over resources; eg both copies trying to read/write a particular file such as a log file. Also, the two execution paths could diverge, leading to incorrect/misleading watch values.

So, preventing such side effects is not really a feasible option here and would rarely be useful anyway. Just expect that the watches can both reflect and affect the code execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM