Unit objectives

In this unit, I’ll show you how to:

  • Install the following software on your computer:
    • The Java Development Kit (JDK)
    • Kotlin
    • IntelliJ Community Edition (CE)
  • Use the Read-Eval-Print-Loop (REPL)
  • Create a Kotlin project in IntelliJ
  • Familiarize yourself with the Kotlin stdlib documentation and where to locate it on the web

Install the JDK

All of the tools you’ll install in this unit require the JDK, so you should install that first. Kotlin is designed to work with JDK 6 and higher. This tutorial was written using JDK 8.

In the section that follows, I’ll walk you the details of how to install the JDK on your Mac. For Windows and Linux systems, I’ve provided links to the specific installation instructions.

If you are running a platform other than MacOS, Windows, or Linux, check out this page for installation instructions and to see if your platform is supported.

Once you install the JDK, you’re ready to install the Kotlin command line compiler, which you’ll need to complete the learning path.

MacOS

To install the JDK on MacOS, I recommend that you use Homebrew. It’s super easy to install and use. The Homebrew page has easy-to-follow installation instructions.

You will also need to install the Xcode tools for MacOS in order to run Homebrew. To see if the Xcode tools are installed on your Mac, go to a Terminal window and run git --version. If you see the following output, then you are good to go:

$ git --version
git version 2.18.0

If not, MacOS will display a dialog asking you to if you want to install XCode tools:

Install XCode tools

Figure 1. This dialog appears if Xcode tools is not installed on your Mac

Click Install. Once the Xcode tools installation is complete, you can install Homebrew.

Go to a Terminal window and run brew cask install java8. When that is finished, to verify the installation, type java -version, and you should see output like this:

$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

Check out the MacOS installation instructions for more information.

Windows

If you’re running Windows, you can find detailed installation instructions here.

Linux

If you’re running Linux, you can find detailed instructions here.

Install Kotlin

Use the Kotlin command line compiler to compile Kotlin code from the command line and to run the Kotlin shell environment, also known as the Read-Eval-Print-Loop or REPL. (After the next section on how to install IntelliJ CE, I’ll explain how to use the REPL.)

Check out this page for instructions on how to install the Kotlin command line compiler for a variety of platforms, including MacOS, Windows, and Linux.

Once you have installed the Kotlin command line compiler, verify the installation from the command line with this command: kotlinc -version. You should see output like this:

$ kotlinc -version
info: kotlinc-jvm 1.2.61 (JRE 1.8.0_181-b13)

Install IntelliJ CE

I’ve used Eclipse for many years, but I’m going to use IntelliJ in this learning path for two reasons:

  • IntelliJ is nice. It has so many really cool features that are better than the Eclipse counterparts. For example, IntelliJ’s code completion feature helps me to type about half as much as I do when I use Eclipse.
  • IntelliJ is from JetBrains and so is Kotlin. Okay, sure, Kotlin is open source, but let’s face it, Kotlin is basically JetBrains’s baby, so using IntelliJ with Kotlin is a good fit.

That said, you don’t have to use IntelliJ for this learning path. You can use any IDE that has Kotlin support.

IntelliJ comes in two editions:

  • Community Edition (CE), which is free and open source
  • Ultimate Edition, which currently costs $500USD for the first year and drops by $100USD each year thereafter until year three

The Ultimate Edition has a 30-day free trial offer if you want to try it out and compare it to CE.

You can:

For each, click Installation instructions and follow the directions to complete the installation.

IntelliJ provides excellent documentation, so I won’t cover how to use it in detail here.

If you’ve never used IntelliJ before, at a minimum be sure to check out this video, “Running IntelliJ IDEA for the First Time”, to get acquainted with it.

Using the Read-Eval-Print-Loop

The Kotlin command line compiler becomes an interactive shell environment (also known as the Read-Eval-Print-Loop or REPL) when you run it with no arguments:

$ kotlinc
Welcome to Kotlin version 1.2.61 (JRE 1.8.0_181-b13)
Type :help for help, :quit for quit
>>>

Now enter the following Kotlin code one line at a time into the REPL:

fun printme(message: String) {
    println(message)
}
fun sayit() {
    printme("Hello, Kotlin!")
}
sayit()

Example 1. Simple Kotlin program

The output looks like this:

$ kotlinc
Welcome to Kotlin version 1.2.61 (JRE 1.8.0_181-b13)
Type :help for help, :quit for quit
>>> fun printme(message: String) {
... println(message)
... }
>>> fun sayit() {
... printme("Hello, Kotlin!")
... }
>>> sayit()
Hello, Kotlin!
>>>

Notice that when you enter a { character, the REPL detects an incomplete block and turns the >>> prompt into ... to indicate a continuation line.

Type :help, and you’ll see a list of available commands like this:

>>> :help
Available commands:
:help                   show this help
:quit                   exit the interpreter
:dump bytecode          dump classes to terminal
:load <file>            load script from specified file
>>>

Type :quit at the >>> prompt, and press Enter to exit the REPL.

Other IDEs

If you plan to use Eclipse or Netbeans, check out these links to install the Kotlin plugins you will need.

Source code for the learning path

I’ve created source code you can clone from my IBM Developer GitHub repo. The source for the learning path is in the Kotlin subdirectory.

Each unit in this path has some source code in a folder marked with the unit number. For example, the code for this unit is in the subdirectory Unit_2/src.

Open a Terminal window, navigate to the directory where you want the code to land, and run this command: git clone https://github.com/jstevenperry/IBM-Developer. You’ll see output like this:

$ pwd
/Users/sperrry/src
$ git clone https://github.com/jstevenperry/IBM-Developer
Cloning into 'IBM-Developer'...
remote: Counting objects: 311, done.
remote: Compressing objects: 100% (160/160), done.
remote: Total 311 (delta 134), reused 302 (delta 128), pack-reused 0
Receiving objects: 100% (311/311), 21.69 MiB | 10.26 MiB/s, done.
Resolving deltas: 100% (134/134), done.
$

Create an IntelliJ project from the source code

Projects make applications easier to build and they provide structure for your source files. When you group your source code, configuration, resource, and other source files together in a project, you get everything together in one place.

Start IntelliJ and choose Create New Project from the Welcome Screen shown in Figure 2.

IntelliJ Welcome Screen

Figure 2. The IntelliJ Welcome screen

If you’ve used IntelliJ before, the last project you loaded will be opened by default. Go to File > Close Project to close it then you’ll see the Welcome screen.

From the New Project dialog, choose Kotlin from the available templates in the left-hand navigation window and Kotlin/JVM from the types in the right-hand window. See Figure 3.

IntelliJ New Project Dialog

Figure 3. The IntelliJ New Project wizard, first dialog

Click the ellipsis (…) next to Project location, navigate to the directory where you cloned the source for the learning path, choose the Kotlin/Unit_2 directory, and click Finish. See Figure 4.

IntelliJ New Project Dialog 2

Figure 4. The IntelliJ New Project wizard, second dialog

When the IDE opens, expand the src directory and open the Example2.kt file in the editor window. The source is shown in Example 2:

package example2

fun sayHello(message: String) {
    println(message)
}

fun main(args: Array<String>) {
    sayHello("Greetings from example 2")
}

Example2.kt. A simple Kotlin source file with runnable main function

To run this code in the IDE, click the run button next to the main function, and choose Run example2.Example2Kt. The code will compile, then run, and the output will appear in the output window at the bottom of the IDE.

Run Example2.kt in IntelliJ

Figure 5. Running the main function

Explore the Kotlin stdlib documentation

The Kotlin Standard Library (called stdlib) provides utilities that you will see as you move through the learning path. You should visit the main documentation page and bookmark it in your browser. You will reference functions and other utilities from the stdlib often throughout the course. If you want more information on a particular stdlib function, make sure to visit the documentation page and deepen your understanding.

Video

In the video, I’ll show you how to:

  • Install the JDK, Kotlin, and IntelliJ for MacOS
  • Clone the source code from GitHub for the learning path
  • Create a project in IntelliJ for the Unit 2 source
  • Run the Example2.kt file in IntelliJ

Previous: Learning path overviewNext: Object- and function-oriented programming concepts and principles