Kotlin — Installation & Setup

Harsh Kumar Khatri
5 min readSep 1, 2020

--

Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of its standard library depends on the Java Class Library, but type inference allows its syntax to be more concise.

In this article I will be covering about:-

  • Benefits of Kotlin
  • Installing and setting up Java JDK.
  • Installing IntelliJ IDEA.
  • Creating and running a hello world program in Kotlin.

Benefits of Kotlin

Kotlin is a new, modern programming language created by programmers, for programmers. It’s focused on clarity, conciseness, and code safety. Below are some benefits which Kotlin has

  • Robust Code:- The creators of Kotlin made various design decisions about the language to help programmers create robust code. Kotlin distinguishes between nullable and non-nullable data types, which helps catch more errors at compile time. Kotlin is strongly typed, and it does a lot to infer the types from your code. It has lambdas, coroutines, and properties, which allow you to write less code with fewer bugs.
  • No runtime overhead:- The standard library is small and tight: it consists mostly of focused extensions to the Java standard library. Heavy use of compile-time inlining means functional constructs like pipelines of map/filter/reduce compile similarly to an imperative version of the same code.
  • Mature Platform:- Kotlin has been around since 2011 and was released as open-source in 2012. It reached version 1.0 in 2016, and since 2017 Kotlin has been an officially supported language for building Android apps. It’s included with the IntelliJ IDEA as well as Android Studio 3.0 and later.
  • Interoperable with Java:- Kotlin code compiles so that you can use Java and Kotlin code side-by-side, and continue to use your favorite Java libraries. You can add Kotlin code to an existing Java program, or if you want to migrate your program completely, IntelliJ IDEA and Android Studio both include tools to migrate existing Java code to Kotlin code.
  • Strong Community:- JetBrains is constantly improving the language. It’s committed to the project, has a large and highly competent team working on it, has a stable business model, and is even converting parts of their own flagship product to use it.

Installing and setting up Java JDK

Before installing the Java JDK you need to see if it is pre-installed in your system or not. You can check this by typing the command given below in your terminal.

javac -version

You can see what the latest version of the JDK is on the Java SE Downloads page. If you have the latest version, skip ahead to Install IntelliJ IDEA.

1: Un-installing the old version:-

  • For Windows, select Control Panel > Add/Remove Programs.
  • For Mac instructions, see Uninstalling the JDK.

2: Downloading the JDK

You can download the JDK for free here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

  1. Click on JDK Download.
  2. Under Downloads, choose the link for the JDK for your operating system.
  3. Accept the license agreement.
  4. Click on the Download button.

3: Install the JDK (for Mac)

From either the Downloads window of the browser or from the file browser, double-click the .dmg file to launch the install file.

  1. A Finder window appears with an icon of an open box and the name of the .pkg file.
  2. Double-click the package icon to launch the installation app, and follow the prompts as they appear.
  3. You might need to enter the administrator password to continue.
  4. After the installation is complete, feel free to delete the .dmg file to save space.

3: Install the JDK and JRE (for Windows)

  1. Run the downloaded installer (for example, jdk-14.0.1_windows-x64_bin.exe), which installs both the JDK and the JRE. By default, the JDK is installed in the C:\Program Files\Java\jdk-14.0.1 directory, but it depends on the latest version.
  2. Accept the defaults, and follow the on-screen instructions to install the JDK.

3: Install the JDK and JRE (for Linux)

https://docs.oracle.com/javase/9/install/installation-jdk-and-jre-linux-platforms.htm#JSJIG-GUID-09D016D5-AB67-4552-9312-3B249180BD0F

4: Add the JDK installation directory to PATH (Windows only)

Windows search the current directory and the directories listed in the PATH environment variable (system variable) for executable programs.

  1. In Settings for Windows, search for edit environment in Find a setting.
  2. Select Edit environment variables for your account in the list of matches.
  3. In the Environment Variables dialog in the User variables section, select Path and click the Edit… button.
  4. Add the path to the JDK’s bin directory, for example, C:\Program Files\Java\jdk-14.0.1\bin, after any existing items.

5: Verify the JDK installation:- To verify that the JDK was installed correctly, type the following commands in a terminal window:

java -version
javac -version

Installing the IntelliJ IDEA

1: Download and install IntelliJ IDEA

Download IntelliJ IDEA for your operating system.

Windows:

  1. Run the ideaIC.exe file that you downloaded.
  2. Follow the instructions in the installation wizard.

Mac:

  1. To mount the macOS disk image, double-click the ideaIC.dmg file that you downloaded.
  2. Copy IntelliJ IDEA to the Applications folder.

Linux:

  1. See Install-Linux-tar.txt in the downloaded .tar.gz file.

For more information on how to install and set up IntelliJ IDEA, check out Install IntelliJ IDEA.

2: Verify your IntelliJ IDEA installation

  1. Start IntelliJ IDEA.
  2. Install any updates and additional content you are prompted for.
  3. Select Configure > Check for Updates until there are no more updates available.

Creating and running a hello world program in Kotlin

Create a Kotlin project so IntelliJ IDEA knows you’re working in Kotlin.

  1. In the Welcome to IntelliJ IDEA window, click Create New Project.
  2. In the New Project pane, select Kotlin in the left-hand navigation.
  3. Select Kotlin/JVM (JVM | IDEA in newer versions) in the right panel and click Next.
  4. Name your project Hello Kotlin.
  5. Click Finish.

You will be writing your hello world kotlin program in Kotlin REPL(Read-Eval-Print Loop). If you don’t have REPL visible on the bottom of you screen follow the steps below.

  1. Select Tools > Kotlin > Kotlin REPL to open the REPL.
  2. Type or paste the code below into the REPL.
fun printHello() {
println("Hello World")
}

printHello()

3. Press Control+Enter (Command+Enter on a Mac). You should see Hello World, as shown below.

Congratulations! You’ve written your first Kotlin program. If you have any doubts in any of the steps above do comment them down. I would be happy to help.

Connect with me on Linkedin:- Harsh Kumar Khatri | LinkedIn

References:-

--

--