Book Image

Learn Kotlin Programming - Second Edition

By : Stephen Samuel, Stefan Bocutiu
Book Image

Learn Kotlin Programming - Second Edition

By: Stephen Samuel, Stefan Bocutiu

Overview of this book

Kotlin is a general-purpose programming language used for developing cross-platform applications. Complete with a comprehensive introduction and projects covering the full set of Kotlin programming features, this book will take you through the fundamentals of Kotlin and get you up to speed in no time. Learn Kotlin Programming covers the installation, tools, and how to write basic programs in Kotlin. You'll learn how to implement object-oriented programming in Kotlin and easily reuse your program or parts of it. The book explains DSL construction, serialization, null safety aspects, and type parameterization to help you build robust apps. You'll learn how to destructure expressions and write your own. You'll then get to grips with building scalable apps by exploring advanced topics such as testing, concurrency, microservices, coroutines, and Kotlin DSL builders. Furthermore, you'll be introduced to the kotlinx.serialization framework, which is used to persist objects in JSON, Protobuf, and other formats. By the end of this book, you'll be well versed with all the new features in Kotlin and will be able to build robust applications skillfully.
Table of Contents (21 chapters)
Free Chapter
1
Section 1: Fundamental Concepts in Kotlin
5
Section 2: Practical Concepts in Kotlin
15
Section 3: Advanced Concepts in Kotlin

IntelliJ and Kotlin

Coding using Vim/nano is not everyone's first choice. Working without the help of an IDE with its code completion, IntelliSense, shortcuts for adding files, or refactoring code can prove challenging depending on how complex the project is.

For a while now, in the JVM world, people's first choice when it comes to their integrated development environment has been IntelliJ. The tool is made by the same company that created Kotlin—JetBrains. Given the integration between the two of them, it would be my first choice of IDE to use, but, as we will see in the next section, it is not the only option.

IntelliJ comes in two versions—Ultimate and Community (free). For the code we will be using over the course of this book, the free version is sufficient. If you don't already have it installed, you can download it from https://www.jetbrains.com/idea/download.

From version 15.0 onward, IntelliJ comes bundled with Kotlin, but if you have an older version, you can still get support for the language by installing the plugin. Just go to Settings | Plugins | Install IntelliJ plugins, and type Kotlin in the search box.

We are going to use the IDE to create a Gradle project with Kotlin enabled, just as we did in the previous section. Once you have started IntelliJ, click Create new project. You will then see a dialog window from which you should select Gradle from the left-hand side section;. Then, check the Kotlin(Java) option from the right-hand side, as shown in the following screenshot:

Selecting a project type

You should already have the system variable JAVA_HOME set up for the tool to pick it up automatically (see the Project SDK at the top of the screenshot). If this isn't the case, click the New button and navigate to where your JDK is. Once you have selected it, you are ready to go to the next step by clicking on the Next button available on the bottom right-hand side of the screen.

The next window presented to you asks you to provide the Group Id and Artifact Id. Let's go with com.programming.kotlin and chapter01, respectively. Once you have entered these fields, you can move to the next step of the process where you tick the Use auto-import flag and Create directories for empty directory roots automatically options. Carry on to the next step, where you will be asked where you wish to store the project on your machine. Set the project location, expand More Settings, type chapter01 for the Module name, and hit the Finish button.

IntelliJ will go on and create the project, and you should see the outcome shown in the following screenshot:

Hello World! basic project

With the kotlin folder selected, right-click, select the New | Package option, and type com.programming.kotlin.chapter01, as shown in the following screenshot:

Setting up the package name

You should see a new folder appear below the kotlin folder, matching what was typed earlier. Right-click on that, choose New | Kotlin File/Class, and type Program.kt, as shown in the following screenshot:

Creating the Program.kt file

We are now ready to start typing our Hello World! string. Use the same code we created earlier in the chapter. You should notice the Kotlin brand icon on the left-hand side of the file editor. If you click on it, you will get the option to run the code, and if you look at the bottom of your IntelliJ window, you should see the text Hello World!, printed out, as shown in the following screenshot:

Hello World! program

Well done! You have written your first Kotlin program. It was easy and quick to set up the project and code, and then to run the program. If you prefer, you can have a Maven rather than a Gradle project. When you choose New | Project, you have to select Maven from the left-hand side and check Create from archetype while selecting org.jetbrains.kotlin:kotlin-archetype-jvm from the list presented, as shown in the following screenshot:

Maven project

As the screenshot shows, the Maven option should be selected from the various archetypes.