Debug Like a Pro in IntelliJ IDEA

Dilan Tharaka
5 min readJun 1, 2021
Photo by hitesh choudhary from Pexels

IntelliJ IDEA is my favorite IDE in Java development. Might be yours too. When developing Java projects (Not only Java) debugging acts a huge role. IntelliJ IDEA debugger provides powerful tools to debug Java applications. The purpose of this article is not to give you an introduction to the Intellij IDEA debugger, but to give you few tricks to use some of the powerful features. So, let’s get started.

Stream Tracing

Stream tracing is an interesting feature in the Intellij IDEA debugger. This feature allows us to debug the behavior of a Java Stream in a graphical way. To demonstrate this, I’m using the following sample code. This has a List of Strings and using stream processing to uppercase the list of words and get the count of words where length is greater than 3.

If we place a debug point in the line having the stream procession part, and run the program. When the debugger hits that line, In the debug tools, we can see this button, “Trace Current Stream Chain” is activated.

When we click on that button, a new window named “Stream Trace” is popping up. This window lists out the stream operations we defined. We can click on each operation and see the transformations doing by that step.

And if we click on the “Flat Mode” button, it gives us a nice representation of the stream procession including all the steps and results of each step.

The following GIF shows the full process,

Don’t forget to try this feature the next time debugging a stream 😎

Non suspending breakpoints

Imagine a situation where you have a loop and you need to check a value of a variable inside the loop in the runtime. What would you do? Console log 🤔 , Nope, since we have the debugger, we can simply place a debug point 😎. But, wait, if that loop iterates thousands of times, the program would stop at the breakpoint thousands of times and you have to observe the value and continue. That is a real pain 😐 Console log then, isn’t is 🙃. Nope, we have this cool feature in the IntelliJ IDEA debugger called non-suspending breakpoints which is really useful in this type of scenario.

To demonstrate this, I developed the same scenario we used before without using streams.

Let’s say, we need to check the word length in line 20. We can simply place a breakpoint. But if our word list is lengthy, this would be a real headache. To avoid this, we can place a breakpoint, right-click on it and untick the Suspend check box.

After you untick the Suspend check box, you would get the following popup,

If you check the Evaluate and log check box, then you can write any piece of code in the bellow text box (this can be expanded using the expand button on the right corner).

Intellij Idea would log the output of the code that we write here. So, in this case, I wrote a line of code to build a String with the word and the word length. Intellij Idea would log this sentence when the debugger hits this line and the debugger won’t stop on that debug point. So, when I debug, the debugger console gives the following output,

As you can see, it has printed all the words and word lengths. Not only for logging but also to change the program status (to change a value of a variable, etc. ) on runtime, we can use this approach. What we have to do is, we have to write the code that we need to change the state of the application in that text box. As an example, if I want to add 1 to the word length at line 21, I can do that in runtime by giving the following code to Elavuate and Log text box.

Then, when we debugged the program, it gives the following output,

Note that the last number 4 is from the print statement of the main method. This means all the words have counted since the word length now equals word length + 1. The full process is shown in the following gif,

So, using this method you can completely change the program behavior in the runtime (in debug time 😁) without compiling and running the code again.

That’s all for today guys. Hope you would use these tricks the next time you debug a code in Intellij Idea and save a lot of time. Happy coding 💻 !!!

And, if you’re a doing frontend development also, check out the following articles to debug like a pro in Chrome Dev Tools,

--

--

Dilan Tharaka

Software engineer curious about how things actually work