What Is a Debugger?
Have you ever experienced a weird bug that left you puzzled? If only you could have recreated the crime scene. Perhaps a time machine that took you into your computer at the moment right before your bug occurred? Imagine having been able to just look at everything your code was doing and watch it fail in slow motion.
Good news! A tool already exists with just such power. It can time travel and help you inspect why your software is failing. It's called a debugger.
Where do I get a debugger? And how does it work?
If you have a Java installation or IDE, you already have a debugger! Java’s various IDEs come with really good debugging interfaces.
It allows you to run your programs as you normally would, but with one important difference: you can pause the program and look into the JVM to inspect the variables set within. The JVM runs the bytecode compiled from your software. It's responsible for controlling execution and keeping track of the variables and objects in your code. Debuggers enable developers to connect to the JVM and tell it what to do next. You can check what variables are set to and examine the methods called to get to that particular point. You can even intervene with how the program is behaving to try things out! This can be useful when trying to understand why your software is failing and tracking down a bug in your code. This is why it's called a debugger. 😉
A debugger gives you a nifty remote control for your JVM. In the top box of the diagram below, you can see how it allows you to control your JVM and inspect what it is doing. Over the rest of the course, you'll become familiar with these controls and a master of your remote control!
Did you notice the acronym JPDA in the corner? The makers of Java defined the Java Platform Debugging Architecture (JPDA), which is a set of guidelines that enable anyone to create a debugger. Fortunately, you don't have to build it yourself.
JPDA includes the JDWP (Java Debugger Wire Protocol) which defines how debuggers talk to JVMs. The JDWP works over a network, even if you're just debugging something on your local machine. As a result debugging an application on a JVM on the other side of the planet is pretty much identical to doing so locally.
Can’t I run my program and print out the variables?
You sure can. But if you’ve ever done it, you know it's a slow cycle, and you’re only going to see the values you choose to print. It's like recreating a crime scene, but only including suspects you guessed were there. You might miss someone and never know! However, if you could go back to that moment in time, all the evidence would be there in front of you, including the variables visible to the scope of your code at that time. 🔍
Install IntelliJ Community Edition
I’m going to use the IntelliJ Community Edition to demonstrate how you can debug in Java. IntelliJ is a powerful and extremely common development environment for Java. You can follow along with your favorite IDE, but the UI may look different and use slightly different names. Let's install it!
Click the link to access the JetBrains website where you can download IntelliJ.
Click on the DOWNLOAD button under IntelliJ IDEA Community Edition.
Once downloaded, run the installer.
Click next on each page, selecting all the defaults.
Once the installation is complete, launch IntelliJ IDEA Community Edition.
Congratulations, you now have the IntelliJ IDE and its built-in debugger!
You can use a debugger to control the execution of a program and inspect the variables it uses, as the program runs. This can be useful when recreating the crime scene of a software bug.
The IntelliJ Community Edition includes a debugger which we'll use in this course.