Java software can certainly be written using an ordinary programmer’s text editor, but Java is a fairly complex, verbose language. Integrated Development Environments (IDEs) have a variety of features that help you deal with this complexity and speed up development.
This worksheet provides a basic introduction to IntelliJ IDEA, a widely-used Java IDE, hereafter referred to simply as IntelliJ.
We assume here that you will be using IntelliJ version 2022.3, on your own PC1. You can download it from https://www.jetbrains.com/idea/download/.
You can use either the Community Edition or Ultimate Edition on your PC, for free. If you opt for the Ultimate Edition you will need a student licence, which you can obtain simply by completing an application form. Note that this licence will allow you to use the full professional versions of other JetBrains tools, which you can use to work with C/C++, Python, databases, etc.
For more information on using IntelliJ, please see the user manual.
It is important to spend some time configuring a development environment so that it works the way you want it to. Follow the steps below to see how some aspects of IntelliJ’s behaviour can be configured.
From the start screen, click on Customize from the menu on the left. Choose your preferred IDE theme (dark and light options are available) and select your preferred font sizes for the IDE and code editing.
Next, click on the All settings… link. Open up the Appearance & Behaviour section of the menu on the left of the Preferences dialog and click on Appearance.
Scroll down to the ‘Tool Windows’ section and tick the checkboxes for ‘Show tool window bars’ and ‘Show tool window numbers’. Click on the Apply button at the bottom right of the dialog to apply these changes. (Note: we assume that you’ll do the same to apply changes after each of the configuration steps outlined below.)
Navigate to Editor ⟶ General ⟶ Appearance in the Preferences list. Tick the checkboxes for ‘Show line numbers’ and ‘Show indent guides’.
Navigate to Editor ⟶ Inspections. This specifies preferences for how IntelliJ inspects and and advises on the quality of your code. For now, we want to reconfigure just one of these inspections.
Expand Java ⟶ Declaration Redundancy from the list of inspections and untick the checkbox for the ‘Declaration access can be weaker’ inspection. This will stop IntelliJ from issuing unnecessary warnings about public class and method definitions.
Now navigate to Editor ⟶ Code Style. Enter 80,100
in the box labelled ‘Visual guides’. This will put vertical guidelines
at 80 and 100 columns in the editing window, which will help to warn
you about excessively long lines of code.
Finally, navigate to Editor ⟶ Code Style ⟶ Java. Spend some time exploring the various settings. You should be able to see their effect in the sample code displayed in the dialog.
Please make sure that you
Click on the OK button at the bottom right to apply any remaining changes and dismiss the dialog.
Let’s assume you wish to work on a solution to Exercise 1, using IntelliJ.
Go back to the start screen and click the New Project button.
Enter a name for the project, such as ex1
, and a directory where
you want the project directory to be located.
Leave the options to ‘Create Git repository’ and ‘Add sample code’ unchecked. Make sure that ‘Java’ and ‘IntelliJ’ are selected as the Language and Build System, respectively.
Finally, select a JDK version from the ‘JDK’ drop-down list. Note that there may be multiple options available here. IntelliJ will try to detect installed JDKs automatically, but you can also add a JDK manually using the Add JDK… menu item. You can even download a JDK from here if you need to.
Click the Create button to create the project directory and open the project in IntelliJ.
After a short delay, you should see the normal IntelliJ interface appear, with the project tool window on the left and the editing area on the right.
In the project tool window, you will see that IntelliJ has created a
folder named src
. This is where you should create the .java
file
needed for the exercise. Right-click on the src
folder and choose
New ⟶ Java Class. In the pop-up dialog, enter
Weight
as the name of the class and press Enter.
With the cursor positioned within the class definition, type main
and press the Tab key. IntelliJ will expand this to a fully-defined but
empty main()
method, within which you can write the required code.
As you type in the code, you’ll find that IntelliJ will help you in
various ways – e.g., by suggesting possible code completions, adding
import
statements for classes you are using in the code, etc.
To run your finished program, either select Run ⟶ Weight.java from the toolbar, or click on one of the small green triangles in the editor margin. Doing the latter will pop up a menu, from which you can choose to either run or debug the program.
After compiling the program, IntelliJ will make the run tool window visible. You can type input into this window and see the resulting program output. You can also rerun the program by clicking on the small green triangle in the run tool window.
For some exercises, you may be required to configure how a program is being run - e.g., to specify command line arguments. You can do this by creating a run configuration. Select Run ⟶ Run… from the toolbar and then click on the ‘Edit configurations’ link.
You can create multiple run configurations within a single project and then choose the one you want from the drop-down list. Clicking on the green triangle in the toolbar will run the currently selected configuration.
Consult the user manual to learn more about IntelliJ.
IntelliJ also has a built-in interactive training facility that can you use to gain experience with more of its features. You can access this by selecting Learn from the start screen.
An old version is available on SoC Linux machines, but we do not recommend that you use it, and this worksheet will not accurately describe that version! ↩︎