From the course: IntelliJ IDEA Community Edition Essential Training

Step through code with breakpoints

From the course: IntelliJ IDEA Community Edition Essential Training

Start my 1-month free trial

Step through code with breakpoints

- All good integrated development environments have tools to help you debug your code. One of the most important tools for this is the break point. A break point led to mark a line of code and suspend the applications execution at that point. While you're in a break point, you can then inspect the applications internal state it's variable values, resource usage and other factors. To demonstrate breakpoints in IntelliJ IDEA, I'll add a bit of code in this for-loop. I'll once again use sout for system out. Now output the value is, and then I'll append to that the value of I. Now to set a break point I'll click in the trough right next to the line number and this icon means that there's a break point. Now I have to run the application in debug mode. You can do that by clicking on the debug button up here, or you can go to the menu and select run, debug main or you can right click on this icon in the trough and choose debug main from there. No matter how you launch the application it will be running in the same debug mode. Now, when I get to that break point the application is suspended. While you're in the break point you can now step through the code using the buttons at the top of this debug window. Step over means execute this line of code and go to the next statement in this scope. Step into means drill down into this method call and debug internally. There's also four step into step out to move up one scope, and run to cursor. I'm going to use step over a few times and each time I click, I'm executing the next line of code. At any time, you can also click on the resume program button over here, or press the associated keyboard shortcut. And when you're done debugging, you can click the stop button. Break points can also be conditional. I'll click on that icon and then in this dialogue, I'll set the condition to I equals five. Notice I'm using the double equals operator, and you're just using whatever syntax is supported in whatever language you're working in. And I'll click done. Now I'll debug again, and I hit the break point again but notice that the value of I is five. And I see that in a few different places. Right here, right here on the looping construct, here on the execution, and also down here in the variables window. And once again, I can continue stepping through the code and you'll see the display of that variable value change in all those same locations. I'll click stop to exit to debug session, and then I can look at my break points by clicking on this button. I see all my break points there. Right now I only have one, I can right click and group them together if I like, or I can remove the break points by clicking on this icon or by pressing the delete key on my keyboard. Break points are the gateway to all sorts of other debugging tools. Once an application is suspended, you can learn all sorts of useful information about its internal state.

Contents