Black Friday Black Friday
Black Friday ultimate sale! Get up to 80 % extra free points! More information here

Lesson 1 - Introduction to Kotlin, the platform and IntelliJ

Kotlin Basic constructs Introduction to Kotlin, the platform and IntelliJ

Welcome to the first lesson of the Kotlin programming course. We'll go step by step through the most basic console programs to GUI applications for the Android platform. In today's Kotlin tutorial, let's talk about the language history, installation and program our first Kotlin application!

The Kotlin language history

Kotlin could be briefly introduced as an improved Java. In 2011, JetBrains, a company developing a number of popular IDEs, announced a new programming language for JVM, Java Runtime Environment. The applications created in it work wherever popular Java is installed, which is one of the reasons why it makes sense to invest time in learning Kotlin. One of the members of the developer team of the new language justified their work by saying that the former Java replacement at the time, Scala, suffered by many flaws such as slow compilation, i.e. slow conversion of Scala into code for the Java environment, and vague support for Android application development. Although Kotlin has different syntax, the compiled code is fully compatible with Java, meaning that Kotlin can also use Java libraries. Since 2017, Kotlin is the official development language for Android and has been supported by a number of major companies including Google. Because it's a relatively new language, it doesn't carry any legacy problems with it and offers a number of modern approaches.

The Kotlin language features

The Kotlin programming language

From the design perspective, it's an object-oriented, strongly-typed compiled language. Because of that it can be put into the same category as Swift, Scala and other similar languages. Kotlin has been, from the very beginning, since the JetBrains company announced its development, open source and that's why the applications written in it work almost on every operation system. Kotlin is backward-compatible.

In Kotlin, we can write anything we would be able to write in Java. Do you want to write an Android app? Use Kotlin. Do you want to program a GUI application in JavaFX/Swing? Use Kotlin. Do you want to create a graphical game? Again, use Kotlin.

Kotlin can be used to create web applications as well. There are several libraries that support Kotlin, such as Spring, Vert.x or JetBrains' official library, Ktor. Frontends can be written using Kotlinx.html.

As is customary when dealing with modern languages, such as Dart, Kotlin allows to compile our source code into the JavaScript language. If you needed to develop in JavaScript for any reason, you can write it in Kotlin, which will be then compiled into JavaScript when finished. This option is still experimental.

Installation

Since Kotlin runs on JVM, it's necessary to have Java SDK installed (we'll click on Accept Licence Agreement and choose the version depending on our OS, "Windows x64" probably most often). When the installation of Java SDK is done, all we have to do is to install the IDE, IntelliJ. IntelliJ comes in two versions, Ultimate Edition and Community Edition. The Ultimate Edition is paid and offers more options for developers. For us, the free Community Edition will be enough.

Downloading the IntelliJ IDE

After downloading IntelliJ and running the installation program, all we have to do is just clicking on the "Next" button. When finished, we'll launch IntelliJ. The following screen, regarding import settings (e.g. keyboard shortcuts) from other IDEs, should appear. We can cancel that.

Importing settings into IntelliJ

We'll Click "Ok" and then choose the appearance of our IDE.

Choosing the design of the IntelliJ IDE

Choose which design suits you the most and then click the "Next" button. We'll continue by clicking on "Next" until the following screen shows up:

IntelliJ Kotlin project

By clicking on "Create New Project", we'll create a new project. A new window should show up, on the left side we can see Kotlin. We'll choose it and click the "Next" button.

A new Kotlin project in the IntelliJ IDE

As the name of the project, we'll fill in HelloWorld and click on 'Finish'.

Naming a Kotlin project

It's also necessary to create a Kotlin file where we'll write our code. We'll right-click on the src folder on the left and select 'New' -> 'Kotlin File/Class'.

Create a new Kotlin file

In the next step, we'll name the file Main.kt and confirm it by clicking "Ok".

Hello World

It's been a tradition that the first program in a new language is called Hello world. It's a program that will print a "Hello world" message to the user or any similar text in any way.

Let's open our Main.kt file and write the following code in:

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

The code declares the main() function, which is the function that is always called at the start of the program. You probably guessed that fun means a function. We don't have to mind args and Array yet, it's there for the case if we wanted to pass some arguments from the command line. The brackets { and } are important. In the basic lessons, you'll always write all your code inside these (in the main() function).

The code in the main() function prints the famous greeting to a special window which you can find below the editor. There, you'll later enter keyboard inputs as well, to which your programs will react. println() is a function that prints text to the console. Functions can have input parameters which you can pass inside parentheses, we separate them by commas. In the case of the println() function, the parameter is the text we want to print. We're going to refer to texts as to strings and wrap them in quotation marks so Kotlin would understand them and didn't misinterpret them as some other commands. You can try to edit the text and print your own message. Of course, we won't include this line printing the greeting in our further programs. However, we'll keep the main() function for every other program in this course.

You can launch your first program by clicking the green "Play" button in the top bar. The printed text will appear in the console below.

Hello World in Kotlin

Kotlin vs Java

We'll end right here for today. You surely get along with IntelliJ by now and know how to run your Kotlin code simply. For those of you who are switching from Java or have better understanding of its principles, I'll list the differences against Kotlin as well. The rest of you can ignore this part, it'd be only confusing for you, we'll explain everything during the course.

What Kotlin has NOT, but Java has

The following features of the Java language has turned out to be problematic and that's why Kotlin hasn't implemented them:

  • Checked Exceptions
  • Primitive types (non-class)
  • Static members
  • Non-private attributes
  • Genericity wildcards
  • Ternary operator

Kotlin HAS but Java hasn't

The following features hasn't been added to Java by its developers yet:

  • Simpler lambda expressions and inline functions
  • Extension functions
  • Null-safety (the end of NullPointerEx¬≠ception :) )
  • Small casts
  • String templates
  • Properties (automatically implemented getters/setters)
  • Primary constructors
  • First-class delegation
  • Automatic data types detection
  • Singletons
  • Declaration-site variances and Type projections (generics)
  • Range expressions
  • Operator overriding
  • Companion objects
  • Data classes
  • Separated interfaces for read-only and mutable collections
  • Coroutines

Now we're really finished. In the next lesson, Variables, type system and parsing in Kotlin, it'll get more interesting. We'll learn how to work with variables and read input from the user.


 

 

Article has been written for you by Samuel Kodytek
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
Thumbnail
All articles in this section
Kotlin basic constructs
Activities (4)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!