What is Kotlin?

Kotlin is a modern programming language that is free and open source. It got its’ name from the Kotlin island that is located near St. Petersburg. It originates from a company named JetBrains that are also developing a well-known development environment IntelliJ IDEA.

Kotlin is a statically typed language. In addition to that, it allows you to combine object-oriented programming features with functional programming ones from the get-go. It is targeting the Java platform (runs on JVM) and is focused on interoperability with Java code. To put it simply, you can without any trouble call Java code from your Kotlin code. This is a great feature allowing to gradually move Java code to Kotlin if wished so. Moreover, you can use Java libraries and frameworks in you Kotlin application.

Kotlin can be used basically wherever Java is used: for server-side development, Android apps and so on. One could say that Kotlin’s key characteristics is to be concise and safe for developers to use.

Now, let’s take a closer look at some of those traits Kotlin is about.

Statically Typed

Kotlin is a statically typed language, just like Java. This simply means that the type of each variable is already known at the compile time.

One of the difference between Kotlin and Java, when it comes to types, is that Kotlin supports type inference. Type inference is the ability to automatically determined the type of a variable from the context. And because of this, a lot of extra verbosity associated with static typing disappears. Following is a simple example of it in Kotlin:

// Define immutable variable x and set it to 1
val x = 1

In the example above Kotlin is automatically able to figure out that the type of the variable x is Int from the context of the code.

In addition to type inference, here are some other wonderful benefits of static typing:

  • Performance– Calling functions is faster. This is because there is no need to figure out which function needs to be called.
  • Reliability– Reduces possible bugs because the compiler checks correctness of the code.
  • Maintainability– You can see what kind of objects the code is working with making it easier to understand unfamiliar code.
  • Tool support– There are various IDE features available like code completion, code refactoring etc.

Object-oriented and Functional

Kotlin allows us to combine object-oriented programming and functional programming. So it’s entirely up to you to decide whichever fits best for the problem you are solving.

To point out, Kotlin has a great set of features to support functional programming from the get-go, like:

  • Function types– Functions can receive other functions as parameters or return other functions.
  • Lambda expressions– You can pass around code with minimum boilerplate.
  • Data classes– You can create immutable value objects with very concise syntax.
  • Various APIs in the standard library for working with objects and collections in the functional style.

Concise

Kotlin has done a great job in thriving to be a concise language. It has removed a lot of boilerplate that makes the code easier to read and understand. You will save a lot of time on just typing alone.

One example where Kotlin has removed tons of boilerplate code is classes. For instance there is no requirement to define getters and setters in a class. You can of-course define those if needed to achieve some custom behaviour. But otherwise Kotlin is able to create those “behind the scenes”.

To demonstrate this, let’s take a look at a simple example of a data class. Data classes are classes that are simply holders of data.

// Define immutable data class Cat. Immutability is achieved by defining the variables name and furColor as val.
data class Cat(val name: String, val furColor: String)

Here we have defined a data class Cat that has two fields: name and furColor. When you create an instance of this class both of the fields will automatically get assigned. There is no need to define anything extra.

When it comes to data classes, the equals, hashCode and toString functions are also generated behind the scenes. To point out, Kotlin has a support for “regular” classes as well. But then you will need to create those functions yourself.

Furthermore, here follows some other cases where Kotlin has managed to make the code more concise:

  • When creating an object, you don’t need to use the new keyword:
// Create a new instance of Cat
val cat = Cat(name="Kitty", furColor="black")
  • Semicolons are not required.
  • Types are inferred automatically.
  • Explicit casts are usually not required. Kotlin is in most cases able to figure those out from the context of the code.

These were just a few examples where Kotlin has done a great job to be more concise. Just remember to keep in mind that concise code is not always good code. Good design and expressive names always play a significant role in making your code readable and easier to maintain.

Safe

Kotlin’s most famous safety measure is probably its’ attempt to eliminate the NullPointerException. If you by any chance haven’t already heard about the famous billion-dollar mistake, you can read a bit more about it here.

So, how do they do that? The solution is actually incredibly simple. To handle possible null values we have a single character, a question mark (?), to use.

And it all starts from defining the fields. Here we have the possibility to mark if a variable can hold a null value or not:

// Define variable name that can be null using the ? and set the value to null
val name: String? = null 
// Define variable name2 that cannot be null. Notice there's no ? after the type String
val name2: String = "" 

Defining the fields as nullable (or not) is just one of the many null safety features Kotlin actually comes with. It’s a wonderful simple feature for a developer to know if we can expect a null value in the code or not. And also to have the possibility to handle all of those cases in a graceful manner.

As a last thing to mention on the safety topic is that Kotlin runs in JVM. This alone comes with various safety guarantees, like: memory safety, preventing buffer overloads and so on.

Conclusion

Kotlin is a modern statically typed language. It allows you to combine object-oriented programming features with functional ones. In addition to that, it is concise and safe for developers to use.

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *