CodeGym /Courses /New Java Syntax /Debugging in IDEA: variables

Debugging in IDEA: variables

New Java Syntax
Level 5 , Lesson 7
Available

1. Watch

When execution of your program stops at a breakpoint or when you're stepping through one instruction at a time, you can look at the values of the variables known at the current location in the program.

Let's write a program that fills a 10-element array with the numbers from 100 to 109. Example:

Debugging in IDEA variables

IntelliJ IDEA is smart enough to display the values of important variables right above the code. In our case, we're talking about the data array variable

Also, at the bottom of the screenshot, we see the Debugger tab open (not Console ). It displays all variables (along with their values) that are known at this place in the program.

If you press F8 10 times, then you will do 5 iterations through the loop (one press for the loop header and one for the loop body). Then you'll get a result like this:

Debugging in IDEA variables 2

We've completed 5 out of 10 iterations of the loop, and you can see that the data array already has 5 values: 100, 101, 102, 103 and 104.

By the way, if you collapse the contents of the array, you can see some more useful variables:

Debugging in IDEA variables 3

2. Changing the values of variables

By the way, if you want to test how your program will behave given certain values of variables, you can simply change the value of any variable right while the program is running (in debug mode).

To do this, right-click on the variable's name or press F2:

Then simply enter the variable's new value and press Enter and that's it:

You just learned at most 5% of all IntelliJ IDEA features. Once you've mastered these, we'll talk about the rest.


3. Executing a snippet of code

You can also execute arbitrary code at any time while your program is running. This is done using the Alt+F8 key combination or the corresponding item in the context menu:

Executing a snippet of code

A special window will open where you can write any code, and that code can use any variables known at the current location in the program's execution!

You can call any methods, say, to make the program display some text on the screen without interrupting its work! Example:

Executing a snippet of code 2
5
Task
New Java Syntax, level 5, lesson 7
Locked
Changing shoes on the move
Practice declaring and changing the values of variables while a program is running: 1. Put a break point on the call to the println method and run the program in debug mode. 2. After execution stops, press F8 exactly 9 times. 3. Look at the current value for the correction variable and set the fahre
5
Task
New Java Syntax, level 5, lesson 7
Locked
Executing a snippet of code
Practice executing a snippet of code while a program is running: 1. Put a break point on the call to the println method and run the program in debug mode. 2. After execution stops, press Alt+F8 and execute the following code: loop.replaceAll("\\W", "").split("fahrenheit")[1] 3. Assign the result of
Comments (39)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Makkli Level 7, Puyallup, United States
17 August 2024
If you just installed IntelliJ IDEA for the first time and you happened to have VSCode (or another IDE) already on your system and you notice that you're bangin' the F8 key to no avail, check your Keymaps in settings. During installation, the installer will have noticed that you have these other IDEs and it will offer to import those settings by default which will change the keymaps. If like me you habitually click Next>Next>Next through installs, you probably changed your keymaps. To set it back to default (on a Windows machine), in Settings>Keymap select "Windows" and apply. I have no idea what you Mac folks need to do but maybe this is enough hint to help.
Schliggerments Level 8, United States
5 June 2024
For those that are having issues with working on the projects through CodeGym plugin via IntelliJ IDE, check out my screen shot. If you have already downloaded the plugin and have utilized your secret key, but are still having issues with synchronizing and performing coding challenges, I may have a solution: 1. Click on the "down carrot" in the right corner of the IDE 2. Click on "Synchronize the Tasks" 3. Select "Yes" on the "Task Synchronization" pop up gump. This worked for me and anytime you restart your IDE and want to work with plugin, perform these steps.
17 May 2024
So I noticed it seems to be a trend of the same problems. I too installed intellij and install the codegym plugin, was meant with locked tasks that are available in the web browser. One way to circumvent this, is to redo the task prior the lock one in the Intellij. It should sync up, and allow to tasks in the sequence to unlock. It worked for me, so it might for you as well.
psc Level 28, Saudi Arabia
15 April 2024
Subject: Issue with Opening Tasks in IntelliJ IDEA Dear CodeGym Support Team, I hope this email finds you well. I am writing to report an issue I have encountered while using CodeGym within IntelliJ IDEA. Despite the tasks being available, I am unable to open them. The task remains stuck in the loading phase, and subsequently, it fails to load at all. Even for older tasks, although they do open, they freeze during loading, forcing me to close and reopen the program in order to work on the code without seeing the task requirements. I have tried troubleshooting the issue by restarting IntelliJ IDEA, updating both CodeGym and IntelliJ IDEA to the latest versions, and ensuring a stable internet connection. However, the problem persists. Could you please assist me in resolving this issue? I rely on CodeGym for my coding practice, and this hindrance is impacting my learning experience. Any guidance or assistance you can provide would be greatly appreciated. Thank you for your attention to this matter. Sincerely, [PSC]
Evgeniia Shabaeva Level 33, Budapest, Hungary
18 March 2024
I should note that hotkeys work differently on different gadgets. For instance, on my Lenovo IdeaPad the F8 key only turns off the airplane mode, which makes the tasks impossible to complete the suggested way. Luckily, I have found an alternative to F8 in the IntelliJ IDEA itself (though it slightly irritates me to have to move the cursor every time). Unfortunately, I have not figured out yet what to click in the program instead of the ALT-F8 combination, as the latter does not work on the laptop either. Would be grateful for a hint.
Evgeniia Shabaeva Level 33, Budapest, Hungary
18 March 2024
Just in case, ALT+F8 stands for the command "evaluate expression" and has a calculator icon in IntelliJ IDEA.
Sumit Rusia Level 13, Ireland
3 March 2024
Why i can start a task in web browser, but it is locked in Intellij?
AngelN Level 6, Manila, Philippines
10 March 2024
Same problem here!
X SS Level 25
22 February 2024
SOLUTION: When I did the wind speed (Which is not on website but on IDE) task from previous topic this was unlocked.
Tomasz Enyeart Level 16, United States
30 January 2024
i have the plugin installed and im logged in with intellij but the tasks 'Changing shoes on the move' and 'Executing a snippet of code' are still locked there. why are they still locked and how do i unlock them. i am able to do the tasks on the website but NOT on the plugin with intellij. and why do i have to mannully unlock them myself
m-troja Level 21, Warsaw, Poland
1 January 2024
Why i can start a task in web browser, but it is locked in Intellij?
Anonymous #10782038 Level 24, United States of America
2 January 2024
Did you ever figure it out? its also locked for me in inteliJ but fine here..
John Squirrels Level 41, San Francisco, Poland
8 January 2024
Please text me in DM.
Abhishek Tripathi Level 72, Rewa, India Expert
22 December 2023
Just click on the left side of the code where the Line numbers are written. 1 | class Solution{ 2 | public static void main(String[] args){ 3 | System.out.println("Anything that you want to print"); 4 | } 5 | } Now just click on the left part of the '|' .