CS 346 (W23)
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

IntelliJ IDEA

An Integrated Development Environment (IDE) is custom software designed to support all aspects of software development. It typically includes a code editor, debugging, testing and profiling tools, and anything else that might be needed to support your workflow.

While you certainly can use command-line tools to build applications, it’s strongly encouraged to use an IDE which provides a large number of advanced features (e.g. debugging, profiling support, auto-completion in code, integration with other systems and so on).

In this course, we’re going to use IntelliJ IDEA, an IDE produces by JetBrains (the company that invented Kotlin), which provides all development functionality, and integrates with all of our tools.

Anything we can do to reduce the friction of common software activities is worth pursuing. This is why we like Integrated Development Environment (IDE)s like IntelliJ – they supports a range of common activities:

  • Producing new code: tools for navigating existing classes, maybe filling in code snippets for us.

  • Reading existing code: browsing classes, maybe diagramming the system to allow us to build a mental model of an existing system.

  • Refactoring: the ability to produce a series of possibly sweeping changes without breaking existing code. e.g. renaming a method, and everywhere it’s invoked; extracing an interface from an existing set of classes.

  • Debugging: visualizations and other tools designed to help diagnose.

  • Profiling: tools to help us understand performance and runtime behaviour.

The following section outlines some features that you should investigate. In the examples below, you can generally copy-paste the code into a main() method in IntelliJ and execute it.

Installation

IntelliJ IDEA can be downloaded from https://www.jetbrains.com/intellij

There are two versions of IntelliJ IDEA: the Community Edition, which is open source, and the Ultimate Edition which is more capable. JetBrains offers free educational licenses to students, which includes a license for IntelliJ IDEA Ultimate if you wish. Either one will work for this course.

Make sure to install the version appropriate for your system architecture (e.g. x86 for an Intel processor, or ARM for Apple M1/M2 processors).

IntelliJ will attempt to use the Java JDK that you installed previously. If intelliJ is unable to locate a suitable JDK, it may complain that it cannot locate a Project JDK (i.e. Java JDK): Project SDK Not Defined}

To fix this, click on Setup SDK and enter details for the JDK that you installed (i.e. where it is installed).

Creating a Project

IntelliJ fully supports Gradle, and we can create a new project directly in the IDE.

From the Splash screen, select Create New Project.

New Project

You will need to supply project parameters.

  • Kotlin as your programming language.,

  • Gradle for your build system.

    • Gradle can use either Groovy or Kotlin as a DSL.

    • Either is fine, though course examples are mostly in Groovy (they were created before Kotlin was widely supported).

  • JDK should point to your installation.

If successful, IntelliJ IDEA will open into the main window to an empty project.

Empty Project

After the project has loaded, use View -> Tools -> Gradle to show the Gradle tasks window, and use build -> build and application -> run to Build and Run respectively.

IntelliJ has a number of windows that it will display by default. You can use Cmd-Hotkey (or Ctrl-Hotkey) to navigate these windows[^3].

  • Project: a list of all files (CMD-1).
  • Structure: methods and properties of the current open class/source file (CMD-7).
  • Source: the current source files (no hotkey).
  • Git: Git status and log (CMD-9) - not shown.
  • Gradle: tasks that are available to run (no hotkey) - not shown.

IntelliJ navigation

Producing New Code

Feature What it does
Live templates Use live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements. Tab
Implement methods of an interface or abstract class Inserts stubs for required methods, with the correct method signature and return types. ^ I
Override methods of a superclass Creates an override method that contains a call to the method of the superclass. ^ O
Code completion Helps you complete the names of classes, methods, fields, and keywords within the visibility scope. ⌃ Space

Running Sample Code

We maintain a public Git repository of the source code shown in lectures. To get a copy, git clone the repository URL. This command, for instance, would create a working copy of the sample code in a directory named cs346.

$ git clone https://git.uwaterloo.ca/j2avery/cs346-public cs346

Each subfolder contains a project built using Gradle and IntelliJ, which should be runnable either from the command-line or from the IDE using Gradle.

Here’s a short video demonstrating how to open the sample code in IntelliJ.

You can build and execute these projects directly in IntelliJ:

  • File -> Open and navigate to the top-level directory containing the build.gradle file. Do NOT open a specific file, just the directory. Click Ok.
  • After the project has loaded, use View -> Tools -> Gradle to show the Gradle tasks window, and use build -> build and application -> run to Build and Run respectively.

IntelliJ with Gradle window open

Reading and Understanding Code

IntelliJ can generate UML diagrams from existing code. These diagrams will reflect the structure of actual classes and methods in your application.

The documentation contains a section on source code navigation that is worth reading carefully.

  • To navigate backwards, press ⌘ [. To navigate forward, press ⌘ ].
  • To navigate to the last edited location, press ⇧ ⌘ ⌫.
  • To move caret between matching code block braces, press ⌃ M.
  • To move the caret to the next word or the previous word, press ⌥ → or ⌥ ←.

There are also built-in dialogs that help you navigate through existing code.

Feature What it does Hotkey
Show recent locations Show the files and sections that have been viewed recently in a dialog, where you can quickly move between them. ⇧ ⌘ E
Show type hierarchy Type hierarchies show parent and child classes of a class. ⌃ H
Show method hierarchy Method hierarchies show subclasses where the method overrides the selected one as well as superclasses or interfaces where the selected method gets overridden. ⇧ ⌘ H