Java Eclipse evaluate expression
You can change variable values on the fly using the Debug perspective. The top right corner of Eclipse should have the variables view from which you can select a variable name and change its value. You can also set conditional breakpoints by selecting the breakpoints view in the top right corner of Eclipse. Right-click on the breakpoint and select breakpoint properties. Check the conditional checkbox and write an expression that only when it evaluates as true does the program suspend during Debugging.
Change Variable Values during Debug
Conditional Breakpoints
Although Display View works; There are two ways else to evaluate an expression.
- In the context of the debugging session, we can write and run custom code to evaluate possibilities. This is done in the Debug Shell. For example, if we need to cross-check the correctness of the sqrt functionality, we could do it in the Debug Shell. On the code, Right-click -> Inspect to see the value.
- You can select an expression then open Context Menu -> Inspect or press CTRL + Shift + I, during debugging, the result will be displayed.
1 - Type your expression inside a method that you are debugging
2 - Select that code
3 - Press CTRL + SHIFT + I
4 - Eclipse will evaluate your expression and show the results in a floating window
There is a Display view as well that can do exactly what you want. Go to Window > Show view > Display
(or Other... if the Display view is not there. In the Display view, you can type any code you want during debug (content assist is available). The current objects and variables are also available to use. After you wrote your code, you have to highlight it, right click and select Execute
or hit CTRL-U
. You may play around with the other possible actions as well.
Besides that, you can also write in the source code during debug and evaluate it using Display as you mentioned, if you have the source for the class you are debugging.