Kotlin Tutorial: What is Kotlin Programming Language

By Swati

By Swati

I’m Swati. I accidentally started testing in 2004, and since then have worked with at least 20 clients in 10 cities and 5 countries and am still counting. I am CSTE and CSQA certified. I love my job and the value it adds to software…

Learn about our editorial policies.
Updated January 20, 2025
Edited by Vijay

Edited by Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.

This Kotlin tutorial explains what is Kotlin Programming Language, its features, fundamentals, and how to write your first program in Kotlin:

Kotlin is a modern language that is concise as well as safe. It also provides a lot of advantages in terms of getting rid of the dreaded Null Pointer Exception.

Kotlin is developed and maintained by Jetbrains – the company that also owns and builds IDEs like Intellij, Android Studio, PyCharm, etc.

Getting Started With Kotlin Language

Kotlin Tutorial

List of Tutorials in This Kotlin Series

Tutorial #1: Kotlin Tutorial: What is Kotlin Programming Language [This Tutorial]
Tutorial #2: How to Use Kotlin Companion Object [With Examples]
Tutorial #3: Kotlin Conditional Statements: When, While, For Loop, If Else
Tutorial #4: Kotlin Data Class: When and How to Use Data Class in Kotlin
Tutorial #5: Kotlin: Enums, Interfaces, and Generics


Kotlin has become a lot more popular and has gained a lot of traction ever since Google announced Kotlin as an official development language for Android.

In this tutorial, we will learn the Kotlin language, by understanding its important features, and getting our hands on writing a basic program in the supported editors.

Official Website: Kotlin

Note on Kotlin Versions:

The latest stable version of the Kotlin release is version 1.5.0 (released on May 5, 2021)
To read more about the details of the release, refer to Kotlin’s official documentation here.

Features of Kotlin

#1) Type Inference: In Kotlin, explicit mention of type can be omitted and the Kotlin compiler takes care of assigning an appropriate type to the variable depending on the type of data assignment that’s done.

Please note, this is always valid if you still want to explicitly define the type. Let’s understand this with the help of an example. In Kotlin, both the below INT declarations are valid.

var x: Int = 5
var x = 5

#2) Java Interoperability: Kotlin is fully interoperable with Java – This simply means that both your Kotlin and Java code is interchangeable and can run side by side.

#3) Clean Syntax: Kotlin has a concise less verbose syntax, as opposed to other languages like Java which is more verbose. For example – the Kotlin code does not require semicolons, you don’t need to specify return types if it’s a void type, etc.

#4) Null Safety: In programming languages like Java, a lot of defensive code is written to ensure that NullPointerException is not occurring in the code.

In Kotlin, a lot of this checking happens at the compile time itself. Whether or not a variable can hold NULL value or not, can be defined at the compile time itself.

For Example:

// will not compile</em>
var x: Int = null

The above will not compile because the INT data type is not NULLABLE. If we want the variable x to hold NULL values as well, then should the data type be changed to INT?

var x: Int? = null
x = 10

Also, now all operations on the nullable variable would need to do a null check at the compile time itself.

// does not compile - due to no null check
println(x + 5)

In order to perform a null check, you can call the safe operator (?) or the !! operator which can be used when we are sure that the variable that’s being used won’t be null.

var x: Int? = 10
println(x!! + 5)

In this case – it will return the result 15.

var x: Int? = null
println(x!! + 5)

If x is null – it would throw a Null pointer exception.

#5) Extension Functions: In Kotlin, you can add new functionalities to an existing class using Extension functions. In most languages for adding new functionality to an existing class, you would inherit the class in a separate class and add new functionality. In Kotlin this can be achieved using extension functions. It’s added like a function to the class but is defined outside that class. For example, to define an extension method to String class

fun String.firstExtensionFunction() : String = "Extension string"

The above code would add a function named firstExtentionFunction to the String class and could be used by all String objects within the class where the extension function is defined.

Let’s see how a String variable can call this function

var x = "Test String"
println(x.firstExtensionFunction())
//Output
"Extension string"

Kotlin Dependencies

Kotlin is supported by all major build tools, namely Maven, Gradle, and Ant, and has dependency libraries available for all of these.

For using Kotlin with Maven – we can add the standard Kotlin library as a dependency.

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>

The version can be specified as appropriate.

Similarly, for using Kotlin with the Gradle build system, we can specify the Kotlin implementation library and add the Kotlin plugin for targeting JVM code.

Hence, in your build.gradle file

  • Add Kotlin JVM plugin
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}
  • Add Kotlin dependency to the dependencies section
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

Writing the First Program

Online IDE: You can just navigate here and start writing basic programs for a hands-on experience. When you open the link, it shows a hello world program already typed in as below.

fun

Other IDEs: Intellij (for backend programming), Android Studio (For Android Programming)

Let’s now see how we can write our first Kotlin program using Intellij editor. For Installing IntelliJ Editor, please refer to our in-depth tutorial here

To write your first Kotlin program on Intellij, please follow the steps below:

  • Create a new project on Intellij and select Kotlin/JVM as an additional library for project setup.
gradle1
  • Enter relevant GroupId and ArtifactID as prompted.
version
  • Select “Use auto-import” to have the Kotlin plugins auto imported.
use1
  • Once the project is ready, navigate to src -> main -> kotlin and create a new file/class named App.kt
class1
  • Add a main() function with a simple print command.
fun main() {
println("Hello World!!")
}

This simple function is an entire Kotlin program having a main() function and a print command.

  • To run this program, click the play button appearing on the left side of the function or from the run configurations in the top ribbon of the Intellij code editor.
funmain1
appkt1
  • The output of the program will be displaying a text:
    “Hello World!!”

Understanding the Bits and Pieces of Hello World program

Let’s now try to slice and dice the “Hello World!” program – we learned to execute using Kotlin language through the online editor as well as through editors like IntelliJ / Android Studio.

fun main() {
println("Hello World!!")
}
  • The entry point of any Kotlin based application is a main() function (this is similar to what we have in Java)
  • Any function in Kotlin is written using the fun keyword.
  • Arguments are optional and can be specified within the parentheses following the function name.

Example:

fun main(args : Array<String>)

The syntax for specifying arguments is – argumentName : DataType

  • Kotlin does not require end statements in semicolon (;) as opposed to mandatory semicolons to end a statement in programming languages like Java.
  • In Kotlin, we don’t need a class to wrap functions. i.e., functions can be directly written in a package without an outer class. (The Kotlin compiler internally creates a wrapper class for this)
  • The return type of the function is Unit here (similar to void in Java) – But this need not be explicitly stated and is assumed whenever not mentioned.

Learning Kotlin Language Fundamentals

Kotlin developers have made learning Kotlin fun and easy through a set of programming exercises called Kotlin Koans – It’s a kind of workshop where you can work as a hands-on set of programs either on the online editor or through editor tools like Intellij or Android Studio.

For anyone familiar with Java, Koans is an extremely easy platform to learn skills on Kotlin.

Official link: Kotlin Koans Plugin

On Intellij or Android Studio, you can install the Koans online educational plugin, by following these steps:

  • The Kotlin Koans plugin can be downloaded from the official IntelliJ link here
  • Once downloaded, you can follow the installation steps to install the plugin.
  • After the plugin is downloaded, the Intellij will restart and you can select the course/tutorial from the welcome screen. Click on the Browse courses link.
Browse courses link
  • Select “Kotlin Koans” as the course
Kotlin Koans1
  • Once the course content is loaded, below is how it looks and how the different concepts can be attempted in a fun and engaging way.
list of tutorials

Frequently Asked Questions

Q #1) How do I get started with Kotlin?

Answer: There are plenty of resources available to learn the Kotlin language. One of the fun ways to learn Kotlin is through the Kotlin Koans educational plugin developed by the Kotlin developers. It’s a step-by-step tutorial to learn the different concepts used across the Kotlin programming language.

Q #2) Is Kotlin good for beginners?

Answer: Kotlin is one of the best languages to learn, due to widespread acceptance of Kotlin as one of the primary development languages – ex Android and good interoperability with Java.

In addition to this, there’s a wide developer community support available for Kotlin, along with plenty of free learning resources.

Q #3) Can I learn Kotlin without learning Java?

Answer: The answer is both Yes and No. For people who already have experience with Java, it’s made in a way that they can easily transition – Moreover all Java code can be converted into equivalent Kotlin code automatically.

Kotlin has a lot of syntaxes borrowed from Python which makes it easier to code and learn. However, since a lot of concepts are similar to Java – Like Kotlin support with JVM it would be easy for Java folks to transition to Kotlin.

Q #4) Why did Google choose Kotlin?

Answer: A few reasons why Google would have chosen Kotlin as one of the officially supported languages for Android can be:

  • Modern & Concise nature of Kotlin language – Kotlin greatly reduces code verbosity, thereby requiring fewer lines of code to achieve more (as compared to other languages like Java).
  • Kotlin is statically typed.
  • Kotlin and Java are fully interoperable.

Q #5) Is Kotlin easier than Java?

Answer: Given a lot of useful features of Kotlin like data classes, functional programming, etc – Kotlin is definitely more powerful than Java. In terms of resources, Kotlin being relatively new, there is less community support but it’s gaining traction fairly fast.

For people just beginning with Android development, it’s recommended to start learning Kotlin through tutorials like Kotlin Koans and also study the Java-related concepts as they come along with the Kotlin learning path.

Conclusion

In this tutorial, we learned about some of the basic features of the Kotlin language, along with writing a “Hello world!” program using the IntelliJ Code Editor.

We also learned about the differences and similarities with programming languages like Java and the wide acceptance of Kotlin as a new primary development language like Google for Android development.

With the growing support for Multiple platform development as well as support for both backend and frontend development, Kotlin is the front runner and has a long way to go in the future.

NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment