Introduction to Kotlin — Modern App Programming Language

Ayush Suman
4 min readAug 1, 2022

--

Photo by Vladislav Bushurov on Unsplash

20 miles west of Saint Petersburg city of Russia, in the middle of the Baltic sea, sits a tiny island, 7.5 miles long and about 1 mile broad. As history goes, this island was taken by Peter I of Russia from the Swedes in 1703. The island is known by the name Kotlin.

The city of Kronstadt was established on this island. And in 1921, the city became the site of the Kronstadt rebellion.

About 90 years later, in 2011, the island again became a notable part of an event. When Jetbrains — the company that made IntelliJ Idea — unveiled their project on a new modern programming language, they decided to name the language Kotlin after the Kotlin island… just like Java was named after the Indonesian island of Java (or perhaps it was named after coffee and not the island).

Today, modern Android apps are developed using the Kotlin programming language. Before 2019, Android apps were developed preferably in Java. But there were a couple of challenges in using Java as the language for your apps.

1. Java is wordy — You have to write a lot of redundant lines of code to get even a simple program running. These redundant pieces of code are generally called boilerplate code.

Take for example this case — you want to write a class that represents a Person. A person with the same name, email and age is basically the same person (for your use case). This type of class is generally called a data class because the objects are only a representation of the data (field values) that they hold. If the data is the same, the objects are considered to be the same,

By default, if you create two objects with the same field values, Java (and Kotlin) would consider them different objects. Obj1 == Obj2 would return false despite them having the same data inside.

Let’s see how you would do that in Java —

public class Person {
private String name;
private String email;
private int age;

public Person(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}

public String getName() {
return name;
}

public String getEmail() {
return email;
}

public int getAge() {
return age;
}

@Override
public String toString() {
return "Person(name: " + name + " + ", email: " + " + email + ", age: " + age + ")";
}

@Override
public int hashCode() {
/* return unique hash for person with different name, email or age */

return name.hashCode() + email.hasCode() + age;
}

@Override
public boolean equals(Object obj) {
/* should return true if objects are of same class and name, email and age are equal */

if (obj != null && obj.getClass() == this.getClass()) {
Person castObj = (Person) obj;

if (this.name.equals(castObj.getName())) return false;
if (this.email.equals(castObj.getEmail())) return false;
if (this.age != castObj.getAge()) return false;
}
return false;
}
}

vs in Kotlin, this would be —

data class Person(val name: String, val email: String, val age: Int)

And this is just one example. Older versions of Java do not support unnamed functions (function definitions without a name) or top-level functions (functions that are not part of a class).

This means if you want to tell your code what to do, let's say, after a click event (this is generally called a callback), you cannot pass this instruction as a function. By design, you would be required to create a class for that, with a function within that contains the instruction.

In Android, here is how a callback for a click event is registered in Java—

OnClickListener onClickListener = new OnClickListener() {
public void onClick(View v) {
// Instruction
}
};

someButton.setOnClickListener(onClickListener);

// or simply

someButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Instruction
}
}
);

vs in Kotlin, this would be —

someButton.setOnClickListener {
// Instruction
}

These examples also explain our second point.

2. Java is not very readable — Any new developer reading a piece of code in Java that they have not written will have a hard time trying to understand the code.

You can perhaps see this for yourself in the examples above.

3. Java does not have null safety — I tried to keep this article jargon-free. But here it is — null safety, our first jargon in the article. To understand this, refer to the brief explanation of null safety.

To overcome these challenges and to make life of developers a little simpler, Kotlin was adopted as the new programming language for Android Development.

Since Android Studio 3.0 release in 2017, Kotlin has been included alongside Java as the alternative to the standard Java compiler. Later in March 2019, Google announced Kotlin as the preferred language for Android app development.

Since Kotlin is interoperable with Java, Kotlin is basically replacing Java outside the Android Development world as well. Using Kotlin for Spring once again makes life a lot simpler — and, if you are an Android developer, it also makes you a full-stack Kotlin developer.

Photo by Christin Hume on Unsplash

This marks the end of this article. A little bit about me, I am a developer in love with the process of building and breaking. I use Medium to share my learnings with the community.

I code in Kotlin (and ofc, Java), Go, Rust, JS, Dart and Python, and you can find my projects on Github, or you can find me on Twitter or Linkedin.

.

PS. I love good music. HMU on Twitter to share your playlists, or to get one from me.

--

--

Ayush Suman
Ayush Suman

Written by Ayush Suman

sharing experiences from what i do :)

Responses (1)