Book Image

Kotlin Quick Start Guide

By : Marko Devcic
Book Image

Kotlin Quick Start Guide

By: Marko Devcic

Overview of this book

Kotlin is a general purpose, object-oriented language that primarily targets the JVM and Android. Intended as a better alternative to Java, its main goals are high interoperability with Java and increased developer productivity. Kotlin is still a new language and this book will help you to learn the core Kotlin features and get you ready for developing applications with Kotlin. This book covers Kotlin features in detail and explains them with practical code examples.You will learn how to set up the environment and take your frst steps with Kotlin and its syntax. We will cover the basics of the language, including functions, variables, and basic data types. With the basics covered, the next chapters show how functions are first-class citizens in Kotlin and deal with the object-oriented side of Kotlin. You will move on to more advanced features of Kotlin. You will explore Kotlin's Standard Library and learn how to work with the Collections API. The book finishes by putting Kotlin in to practice, showing how to build a desktop app. By the end of this book, you will be confident enough to use Kotlin for your next project.
Table of Contents (15 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Setting up the development environment


Although you could use a basic text editor for writing Kotlin source code and compile it using the command-line tools, you will make your life easier if you use an IDE for coding. IDE can provide code completion, syntax highlighting, stepping through code while debugging, refactoring, and more.

IntelliJ IDEA is probably the most popular IDE for Java today. The IDE comes from the same company that created Kotlin and, of course, Kotlin is a first class citizen inside IntelliJ. This book uses IntelliJ, and it is recommended that you use it as well. But other popular IDEs for Java could also work, such as Eclipse or NetBeans (a Kotlin plugin would be needed). The great thing about IntelliJ is that the latest versions already come with a Kotlin plugin pre-installed, so the IDE is ready for Kotin development out of the box. This plugin already has a Kotlin compiler and also enables Kotlin syntax highlighting inside the code editor.

Another benefit of using IntelliJ is that it comes with a Java to Kotlin converter. If you have some Java code, or you are working in a mixed (both Java and Kotlin) project, you can convert Java code to Kotlin with the click of a button. Some minor changes might be needed in the resulting Kotlin code but in general, this converter works reasonably well.

IntelliJ has a paid for Ultimate version and a free Community version. The Express version has everything needed for Kotlin development and can be downloaded from the JetBrains website https://www.jetbrains.com/idea/download/. It's available for all platforms (Mac, Linux, and Windows).

Finally, if you are just trying out the syntax or just getting a feel for the language, there is a free online compiler and IDE available at https://try.kotlinlang.org/.

What's great about this online IDE is that it has plenty of examples of Kotlin features. There is also a section called Koans which has smaller code problems with the goal of teaching Kotlin. So, it might be worth checking it out.

Now that we have an IDE installed, let's get the first taste of Kotlin. Inside Intellij, start a new project and select Kotlin from the bar on the left and then select the Kotlin/JVM option, as seen in the following screenshot:

On the next screen, enter your project name and select KotlinJavaRuntime in the Use library section.

This will be enough to compile and run Kotlin code from the IDE. For smaller examples, setting up Maven or Gradle would be an overkill. 

Now that we have a Kotlin project, let's add some Kotlin code: the standard Hello World printed to the console. Add a file named Main.kt inside the src folder.

Inside that file add the following code:

fun main(args: Array<String>) {
   println("Hello World!")
}

Now, there should be a little green icon in the gutter of the text editor and if you click it you should see "Hello World!" in the output.