Starting journey with Kotlin

Adam Chodera
3 min readMay 20, 2017

A few days ago during Google I/O 17, the Android team announced that they will be officially supporting Kotlin in Android ecosystem! That’s great news for Android community and additional motivation to start learning this modern programming language that runs on the Java Virtual Machine. Please keep in mind that currently the Kotlin can be compiled to JavaScript source code and we can expect that in future we’ll be able to run Kotlin also on iOS platform! So bright future in here. Another cool thing is that we can mix Kotlin with Java in the same project without a hassle, as the integration is seamless.

Taking all above into account I’ve decided to start learning Kotlin on daily basis from today. I’m Android Developer with about 4 years of commercial experience using Java and currently I’m working as a freelancer so don’t hesitate to contact me if you’re looking for a solid developer.

I will be posting things I’ve learned much about once a week so you can learn Kotlin with me! All comments and suggestion are more than welcome.

Talk is cheap. Show me the code.

.. as Linus Torvalds said and that’s fair. Let’s first say hello.

fun main(args: Array<String>) {
println(“Hello, world!”)
}

As we can see code looks similarly to Java but let’s jump into differences:

  • Keyword fun indicates that main is a function
  • Unlike in Java, first we declare variable name, then put : character and finally the type of that variable
  • Semicolons ; are optional in Kotlin
  • Wrapper method println which uses standardSystem.out.println

Above was just a warm up. Next we have the max method in Java:

public final int max(int a, int b) {
return a > b ? a : b;
}

I belive there’s not need to describe that so let’s see that written in Kotlin:

fun max(a: Int, b: Int) = if (a > b) a else b

There are some new concepts here:

  • we could skip brackets {}
  • no need to define return type
  • if returns a value so there is no need to write ? and : to handle returning values
  • Int instead of Java’s primitive int

Running Kotlin

For beginners it’s nice that we can play with Kotlin in web browser: https://try.kotlinlang.org

But that’s not good enough for us after a while. With currently available Android Studio 3.0 Canary 1 we have needed plugin for Kotlin pre-bundled so let’s open AS 3.0 and create a new project.

As you can see there is new option called “Include Kotlin support” which I’ve already enabled. Next steps are the same as in previous version of Android Studio.

I created empty Activity and that what I’ve seen:

package pl.adamchodera.learningkotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

The first few lines with package and import keywords are very standard to Java expect missing (and not needed) semicolons.

Next there is class keyword followed by our Activity name, then : character instead of extends we used to write when extending classes in Java. Please note that right now we’re writing code in Kotlin and also extending class written in Java! How mighty is that? — you can ask and I will tell that it’s just a begging to our journey with Kotlin.

Instead of @Override annotation we should simply inline override keyword with method declaration like in example above.

The last difference I want to tackle today is ? character next toBundle parameter. That indicates that this particular variable parameter can be null.

Thank you for reading all along and I hope you enjoyed it and will check out my next blog post about Kotlin in Android development!

Next week we’ll learn more details about language syntax and some cool features we can use to ease our busy life.

--

--

Adam Chodera

Freelance Android Developer. https://adamchodera.pl My passion combined with knowledge creates great user experience.