Taking Kotlin for a Ride

Setting up Kotlin

The rest of the course will focus on the syntax and the semantics of the Kotlin language. To thoroughly learn the concepts, you’ll want to practice the examples. This section will show you how to set up and verify the necessary environment on your system.

Once you key in your code, Kotlin offers a variety of choices to execute code. Unlike Java, you don’t have to compile every single line of code. You can run the code as is, directly from the source code in a file if you like. Alternatively, you may create class files and then reuse the binary to execute other classes or Kotlin files. While in the middle of writing code, you can fire up the Kotlin shell to experiment with the behavior of a little code snippet. You may run your code within the JVM, execute it on a JavaScript engine within Node.js or the browser, or run it on Android devices, and you can also run it on native targets like iOS and WebAssembly.

That’s a lot of choices, but before we can exercise any of them, we need the SDK, so let’s get that installed first.

Installing the Kotlin SDK

If you’re using a recent version of IntelliJ IDEA, then Kotlin comes bundled with the IDE. If you plan to compile and run code only from within that IDE, you’re all set.

But even though we all use IDEs, it’s good to start by learning how to compile and run on the command line, as it gives you full exposure to building the code. Install the standalone compiler from the Download Compiler link from the Kotlin website. This will take you to the Working with the Command-Line Compiler page. You may download and unzip the zip files to install, using Homebrew or one of the other options, depending on your operating system. You’ll also need JDK 1.68 or later on your system.

If you’d like to compile the code using build tools like Maven or Gradle, then refer to the instructions on the Kotlin website. Later in the course, when we’re ready to write tests and intermix Kotlin with Java, we’ll make use of Maven and Gradle project files.

Verifying the installation

Once you install and set the path to where Kotlin is installed, verify all is set by running the following command:

kotlinc-jvm -version

The output from that command, if Kotlin has been installed properly, will show the version number, like so:

info: kotlinc-jvm 1.3.41 (JRE 12.0.2+10)

Don’t worry about the specific JRE version in the output. The output you’ll see on your system will reflect the version of Java you have already installed.

Once you’ve verified the installation, move to the next lesson to write a small piece of Kotlin code and execute it.

Get hands-on with 1200+ tech skills courses.