Understanding Kotlin Language

Vatsal Bajpai
3 min readJul 30, 2016

--

Let’s target a few questions that everyone has in their mind sprouting from the moment they hear about Kotlin.

What is Kotlin?

Statically typed programming language for the JVM, Android and the browser and 100% interoperable with Java™.

Static typed languages are those in which type checking is done at compile-time. Let us have a look at the code and understand the components:

val text = “Made with Kotlin”

Here as you can notice the value is created and assigned the text “Made with Kotlin” but we didn’t specify the type. As kotlin is statically typed the compiler will do a type check during compilation.

So now let’s discuss why we need Kotlin and what are it’s features.

Kotlin has many features but let’s broadly classify into Concise, Safe and Interoperable with Java.

Concise

If you look at the code most of the verbosity of Java is eliminated. Concise code takes less time to write and to read so this will improve your productivity. Also if you look at

class KotlinActivity : AppCompatActivity()

The colon : has replaced extends and implements.

No more SemiColons;

No more semicolons at the end of statements.
P.S no more trolls about missing semicolons :P

fun init()

No it’s not saying to leave code and go have fun. This is how we write functions. We don’t need return type anymore if we don’t need it. But if we need it so we can do this:

private fun getName() : String {
//...
}

Values and Variable

val name = "KAndy" // a value
var days = 3 // a variable
val name : String = "KAndy" // explicit defination

Safe

Kotlin’s type system is aimed at eliminating the danger of null references from code, also known as the The Billion Dollar Mistake.

In Java this would be the equivalent of a NullPointerException or NPE for short.

In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references). For example, a regular variable of type String can not hold null:

var name: String = "KAndy"
name = null // compilation error

To allow nulls, we can declare a variable as nullable string, written String?:

var name: String? = "KAndy"
name = null // ok

With ? we made sure that b can be null but what ever it is used for later will be ignored if null.

val len = name.length // if name is null it is ignored

But we still need to access that property, right? There are a few ways of doing that.

Safe Calls?:

First option is the safe call operator, written ?.

name?.length

This will return length if name is not null and if it’s null it will return a null.

Elvis Operator ?:

Second option is Elvis Operator. When we have a nullable reference ref, we can say “if ref is not null, use it, otherwise use some non-null value fer”:

val l = name?.length ?: -1

It means that return length if name is not-null but if it is null then return -1

The !! Operator

The third option is for NPE-lovers. We can write name!!, and this will return a non-null value of name (e.g., a String in our example) or throw an NPE if name is null:

val len = name!!.length

Note: if you want to know more about null safety, checkout this link: https://kotlinlang.org/docs/reference/null-safety.html

Repository

https://github.com/code-crusher/learn-kotlin

Conclusion

This is just a small part of some Kotlin concepts. If you want to learn more about these concepts, checkout this link which I consider really useful:

http://kotlinlang.org/docs/reference/

Twitter: https://twitter.com/Vatsal__Bajpai

See you soon in the upcoming stories :)

--

--

Vatsal Bajpai

Engineer at @sliceit, Ex - @BranchMetrics | Building products for billions