Kotlin Tutorial 1 — Introduction
Welcome to the Kotlin Tutorial. This is the first written tutorial series on the Internet that covers all the key basics you will need to know about the Kotlin programming language. The main prerequisite for this tutorial is that you have a basic understanding of programming (already know one programming language — Python, Java, Java Script etc). Despite the tutorial being geared toward Python developers it doesn’t matter if you know a different programming language. Note that the tutorial assumes you will be doing Kotlin development on a PC running Linux Mint 18 (Main edition — 64 bit).
Throughout the series the following tools will be used:
- Atom 1.12.5 (programmer’s text editor)
- IntelliJ Community Edition 2016.3 (IDE)
- Gnome Terminal (terminal emulator)
- Kotlin 1.0.5–2 (programming language, includes tools like the compiler)
SDK Man
Kotlin is a pragmatic (industrial), statically typed, multi paradigm (procedural, OO, functional), and general purpose programming language created by Jet Brains in July 2011. The language was created with the aim to make it easier to develop software (of all sizes) that is easier to maintain, and quicker to develop (more productive). Often Kotlin will be used to develop server side (eg micro services), database, and mobile software (Android applications and libraries) as well as test software.
Can also find Kotlin being used to develop desktop applications, scripting for software, DSL’s (Domain Specific Languages), and to a lesser degree front end web applications / websites, even games (eg using libGDX). Jet Brains have plans to expand the use of Kotlin to developing embedded programs (most likely running on ARM micro-controllers without a garbage collector), however that is currently in the investigation phase.
Some of the major companies using Kotlin include the following:
- Gradle
- Basecamp (creator of Ruby on Rails)
- American Express
- Square
- Jet Brains (uses Kotlin in various products including it’s flagship product IntelliJ)
- Pivotal
- 3D Robotics
- Expedia
Install Kotlin
Kotlin will be installed via SDK Man, which is a tool that allows software development tools to be easily installed/uninstalled (similar to Synaptic which is included in Linux Mint).
- Open the terminal with Ctrl+Alt+t (keyboard shortcut)
- Enter the following to install SDK Man: curl -s “https://get.sdkman.io" | bash
- Close the terminal and open up a new one
- Check that SDK Man is installed by running the following: sdk version
- Install Kotlin by running the following: sdk install kotlin 1.0.5–2
- When the “Do you want kotlin 1.0.5–2 to be set as default?” prompt appears enter in Y, then press Enter
- Check that Kotlin is installed by running the following: which kotlinc
Install Atom
Atom is a simple programmer’s text editor that has support for a wide variety of programming languages via its extensive plug-in system and has built-in Git support.
- Open terminal
- Change directory to ~/Desktop
- Download the deb file by running the following: wget https://atom.io/download/deb
- On the desktop double click on the deb file to install Atom
- Exit the Package Installer
- Close the terminal
- Click on the Menu button to show the Applications Menu
- In the search box enter in atom
- Press Enter to start Atom
Setup Atom
Some plug-ins will need to be installed so that Atom supports Kotlin.
- In Atom go to Edit → Preferences
- Select the Install section
- Install the following packages: file-icons, git-plus, language-kotlin, language-kotlin-ide, linter, linter-kotlin, script
- Click the Themes button
- Install the following package: dracula-theme
- Select the Themes section
- Change UI Theme to One Light
- Change Syntax Theme to Dracula
- Select the Core section
- Under Send Telemetry to the Atom Team select Do not send any telemetry data
- Select Editor section
- Change Tab Length to 4
- Change Tab Type to soft
- Set Font Family to roboto
- Set Font Size to 17
- Close the Settings tab (Ctrl+w)
- Exit Atom
First Steps
Open a terminal. What do you think will happen when kotlinc (Kotlin Compiler) is run in the terminal?
- A) Compiler throws a Wobbly
- B) Basic help information is displayed
- C) A segmentation fault occurs (memory loss)
- D) Enters an interactive REPL environment
- E) Compiler enters a curses session
Correct answer is: Enters an interactive REPL environment.
Enter the Kotlin REPL by running kotlinc. Type the following Kotlin code into the REPL:
Press Enter to run the code. You should see numbers being outputted to the screen. Now type in the following code:
Again press Enter. You should now have something similar to the following:
As you can see the Kotlin REPL is very similar to the Python interactive session below:
A more fully featured Kotlin REPL can be used in IntelliJ, below is a teaser:
Conclusion
That concludes the tutorial introduction which will have you setup for part of the tutorial series. Next tutorial will look at Kotlin’s data types (including its approach to nulls) in Kotlin Tutorial 2 — Basic Data Types.
TOC
- Kotlin Tutorial 1 — Introduction
- Kotlin Tutorial 2 — Basic Data Types
- Kotlin Tutorial 3 — Basic Operators
- Kotlin Tutorial 4 — Handling Input And Output
- Kotlin Tutorial 5 — Basic Collections
- Kotlin Tutorial 6 — Control Flow
- Kotlin Tutorial 7 — Functions
- Kotlin Tutorial 8 — Modularity
- Kotlin Tutorial 9 — Using An IDE
- Kotlin Tutorial 10 — Classes
- Kotlin Tutorial 11 — Inheritance
- Kotlin Tutorial 12 — Encapsulation And Polymorphism
- Kotlin Tutorial 13 — Generics