CodeGym /Courses /New Java Syntax /Debugging in IDEA

Debugging in IDEA

New Java Syntax
Level 5 , Lesson 6
Available

1. Bug

Programmers have their own slang, though many consider it to be technical jargon. In any case, you cannot avoid getting to know it. You need to dive into the detail. So let's dive in.

One of the first words you will get to know is "bug", i.e. an insect. In the context of software development, this word means an error in a program, an instance of the program doing something wrong or not quite right. Or simply working strangely.

But if a programmer thinks the program, despite its odd behavior, is doing exactly what it is supposed to, then he or she usually declares something like "it's not a bug, it's a feature". Which has spawned a bunch of internet memes.

In general, there can be any number of reasons for a software defect: anything from errors in the program's logic, typos, and incorrect program architecture, right up to problems in the compiler. In any case, programmers need to fix both real bugs and any other "shortcomings" in their programs.

The history of the word "bug"

The most common version of the origin of the word "bug" is something of a legend.

In September 1945, scientists at Harvard University were testing one of the first computers, the Mark II. The computer wasn't working properly, and in the process of checking all the boards, they found a moth stuck between the contacts of an electromechanical relay.

The extracted insect was taped into a technical log, accompanied by this inscription: "First actual case of bug being found."

This funny story is believed to be the beginning of the use of the word "bug" to mean an error, and the word "debug" has become synonymous with eliminating bugs.


2. Debugging the program

To fix bugs in their programs, programmers use special programs called debuggers. Some of these programs even know how to debug machine code.

Java programmers use IDEs for debugging their programs. Such as IntelliJ IDEA, Eclipse, and NetBeans. IntelliJ IDEA is by far the most powerful IDE, so we'll walk through the debugging process using it as an example.

IntelliJ IDEA can run your program in two modes:

Execution modes Toolbar icon Hotkeys
Normal execution Shift+F10
Start in debug mode Shift+F9

You are already familiar with normal execution: the program starts, runs, and exits. But debug mode has a lot of surprises in store for you.

Debug mode

Debug mode lets you to walk step by step through your entire program. Or more accurately, it lets you move line by line. What's more, you can observe the values of the variables at each step of the program (after each line of code is executed). And you can even change their values!

To gain even a minimal grasp of debugging a program, you need to learn three things:

  • Breakpoints
  • Step-by-step execution
  • Inspecting the value of variables

3. Breakpoints

The IDE lets you place special markers called breakpoints in your code. Every time a program running in debug mode reaches a line marked with a breakpoint, execution will pause.

To put a breakpoint on a specific line, you just need to click to the left of the line in IDEA. Example:

Breakpoints IntelliJ IDEA

The line will be marked with a breakpoint, and IntelliJ IDEA will highlight it in red:

marked with a breakpoint

A second mouse click on the pane to the left of the code will remove the breakpoint.

A breakpoint can also be placed on the current line simply by using the hotkey combination Ctrl+F8. Pressing Ctrl+F8 again on a line that already has a breakpoint will delete it.


4. Start the program in debug mode

If you have at least one breakpoint in your program, you can run the program in debug mode by pressing Shift+F9 or clicking the "bug icon".

After starting in debug mode, the program runs as usual. But as soon as it reaches a line of code marked with a breakpoint, it will pause. Example:

Start the program in debug mode

In the upper half of the screenshot, you see program code with two breakpoints. The program's execution stopped on line 5, which is marked with a blue line. Line 5 has not been executed yet: nothing has been output to the console yet.

In the lower half of the screen, you see the debug panes: the Debugger pane, the Console pane, and a set of buttons for debug mode.

You can unpause your program (i.e. continue execution) by pressing the Resume Program button on the bottom left pane (or press F9).

Start the program in debug mode 3

If you press this button (or F9), the program will continue to run until it encounters the next breakpoint or ends. Here's what we see after clicking the button:

Start the program in debug mode 4

The program stopped at the second breakpoint, and the words Hello and and can be seen in the console. This is a sign that we have executed only two of the three lines that display output on the screen.


5. Step-by-step execution

If your program is running in debug mode, you can also step through it: one step isone line. There are two hotkeys for step-by-step execution: F7 and F8: each causes the current line of code to be executed. But first, you still have to stop your program with a breakpoint.

If you want to execute your program line by line, you need to put breakpoint at the beginning of the main() method and run it in debug mode.

When the program stops, you can start executing it line by line. One press of the F8 key executes one line.

This is what our program looks like after it stops and we press the F8 key once:

Start the program in debug mode. Step-by-step execution

The first line of the main method has already been executed, and the current line is the second line. You can also see at the bottom of the screenshot that the word Hello is already displayed on the screen.


6. Step-by-step execution with stepping into methods

If you have written your own methods in the program and you want execution to go inside your methods in debug mode, i.e. you want to "step into the method", then you need to press F7 rather than F8.

Let's say you step through the program and are now stopped at line 4. If you press F8, IDEA will simply execute the fourth line and move on to the fifth.

Step-by-step execution with stepping into methods 2

But if you press F7, IDEA will step into the main2() method:

Step-by-step execution with stepping into methods 3

It's very simple. If you don't really care what happens inside a method or how, then you press F8. If it is important, then press F7 and step through all its code.

Comments (15)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Abdul Lukman Level 6, Hyderabad
15 May 2025
📍 Placing Breakpoint Traps Before entering battle, Senpai-chan places Breakpoints — traps that stop the code-monsters dead in their tracks. How to place a trap: 👉 Click next to the line numbers or press Ctrl + F8. A red rune 🔴 appears — that’s your breakpoint! Senpai-chan: "I’ve set my trap card… let’s duel!" ⚔️ Battle in Debug Mode Once in Debug Mode, the code runs until it hits your trap (breakpoint) — and then, time freezes. Senpai-chan gazes into the Debugger HUD, seeing all the variables' HP, MP, and positions. Senpai-chan: "I can see the chicken’s spawn value… it was meant to be a creeper!" 👣 Step-By-Step Combat You got two special moves here: F8 (Step Over) — perform a light slash. Skips to the next line without diving into function calls. F7 (Step Into) — a heavy thrust! Dives straight into the method to hunt the bug demon hiding inside. Senpai-chan: "I choose… F7! No evil escapes my sight!" Inside the method lies the truth: the evil chicken summoning code. 💥 Eliminating the Bug Demon Once you spot the corrupted line, you strike true — fix the value, save the world (and your mod). Senpai-chan: "Begone, cluck demon! Set mob type to Creeper!" Change the variable. Hit Resume (F9) to let the code continue. The mod runs again… and BOOM 💣 — creepers go flying. Victory Theme Plays 🎶 🌸 Senpai’s Debugging Wisdom Scroll Bug = code demon Breakpoint = magic trap Shift + F9 = enter Debug Realm F8 = skip a move F7 = dive inside the function dungeon F9 = continue battle Narrator voice: "And thus, Senpai-chan secured peace in CodeCraft… for now. But more bugs lurk in the shadows… and the next episode awaits." To be continued… ⚔️✨
Abdul Lukman Level 6, Hyderabad
15 May 2025
HERE IN MINECRAFT WAY: 📜✨ Episode 1: The Tale of the Bug Demon! Narrator voice: In the legendary world of CodeCraft, where brave programmers wield keyboards like swords and IDEs are sacred temples… a dark evil lurks in the shadows — known only as… the Bug Demon! 🐛👹 🐛 The Bug Demon Appears! Our hero, Senpai-chan, was coding their latest Minecraft plugin mod — a katana that shoots creepers into the air. But… something went wrong. The mod summoned a chicken instead! Senpai-chan: “NANI?! What foul beast dares defy my code?!” That’s a bug, young Senpai — a sneaky error demon hiding in your code, corrupting your program’s logic! 🛡️ Summoning the Debugger Blade! To fight back, Senpai-chan unsheathes their legendary weapon: The Debugger Blade (a.k.a. IntelliJ IDEA). It has two fighting stances:
Evgeniia Shabaeva Level 41, Budapest, Hungary
18 March 2024
I still don't understand why (in section 4 of this lesson) the program is said to output "helloand" if we put a breakpoint at the line which says System.out.println("Hello"); Why does not is stop executing itself immediately?
Grzesiek Level 9, Poland
29 March 2024
First breakpoint is on word "Hello" and the program stop immediately when it occurs, then the program was resumed (F9) and worked until next breakpoint occurs (do every line of code between 1st and 2nd breakpoint).
Radian Hoque Level 7, Footscray, Australia
28 January 2024
In the debug and breakpoint example it says the breakpoint is at line 5 but the image shows it is at line 10. I reckon its a mistake and if so it should be fixed to avoid confusion
Anonymous #11225109 Level 5, United States of America, United States
5 February 2023
i'm still looking where i can find the console, in my screen I can't see it.. Could someone help please? thanks
John Squirrels Level 41, San Francisco, Poland
9 February 2023
Please text me in DM.
Joey M Level 7, Owings Mills, United States
15 September 2022
very nice lesson
Anonymous #11023388 Level 5, Poughkeepsie, United States
24 June 2022
Credit Grace Hopper for the bug term
RolandHut Level 23, Groningen, Netherlands
28 June 2022
"Bug" as a term for a defect predates computers. Grace' report is a great anecdote, it is not the source of the term. If it were it wouldn't make sense, "First actual case of bug being found." even implies that "bug" was already an established term.
Le Phan Level 14, Véry, France
22 December 2021
This tutorial is specified for those who use Windows with lots of shortcut and a trim of corresponding mouse click. What about a user in MacOS or Linux as the shorcut in Win does not cope well with these two OS?
Jonaskinny Level 25, Redondo Beach, United States
28 January 2022
Work for me on Linux
CaseyMacy Level 22, Denver, United States
3 February 2022
Kind of a pain for Mac users
John Squirrels Level 41, San Francisco, Poland
4 February 2022
Keyboard Shortcuts: macOS Keymap
Joey M Level 7, Owings Mills, United States
13 December 2021
Very good explanation of the debugger in IJ. Very helpful tool in array, 2D Array, ArrayList etc