I started converting my first project to Kotlin just a couple of months ago and literally after only a couple of hours I immediately saw how cool Kotlin was. To put this in perspective, I started writing Android apps with the release of Froyo back in the Eclipse days so of course I was giddy about a complete language change. Along the way I’ve picked up some different languages to write Android apps like PhoneGap, Xamarin, and Rhomobile (don’t laugh) but Kotlin is the first to be officially supported by Google. Now a lot of people like to say Kotlin is a lot like Swift but I want to stop you right there and tell you to reorder that. Kotlin was released back in 2012 a full 2 years before Swift appeared. So yes, it is true a lot of Swift syntax looks like Kotlin, and that is a good thing. To make the transition even easier, Kotlin is also interoperable with Java so we can still use 3rd party libraries that are written in Java. I plan to update this post periodically as I use Kotlin more and more in my project and use it as sort of a guide to show the differences and downfalls of the language.

Creating an Activity

NOTES: Notice Kotlin does not have a void return type. Functions are declared using the fun keyword and there are no semicolons at the end of lines. The Bundle? means that there is a possibility that the Bundle code be null.

Creating a TextView

NOTES: The layout has been binded using synthetic in the import statement. This allows us to reference any view in the xml by name. We no longer need findViewById.

Creating a Button

NOTES: The layout has been binded using synthetic in the import statement. This allows us to reference any view in the xml by name. We no longer need findViewById.

Variables

NOTES: Kotlin variables do not require a type and it is recommended to use val as much as possible.

Creating an EditText

NOTES: The layout has been binded using synthetic in the import statement. This allows us to reference any view in the xml by name. We no longer need findViewById.

Null Safety

Now this is something totally new. Kotlin also helps us avoid NullPointerExceptions that are a common problem with Java.

Raw Strings

One of my favorite things about Kotlin is no longer having to escape special characters. You can put nearly anything into a raw string and still call common methods like toUppercase() on it.

Methods

NOTES: Return types are declared towards the end of the method declaration in Kotlin and there is no keyword needed for void return types.

Switch Statements

Optional Parameters

Surprise, we don’t have this in Java.

Arrays

Classes

NOTES: Notice Kotlin classes do not need getters and setters but in order to access setters, you need to use var in the constructor arguments. open in the Kotlin class signifies that it can inherited, by default no Kotlin class be inherited. The open in the shoot() method signifies that it can be overridden. Significantly less code!

Inheritance

Lambdas

This is something new. Lambdas are basically functions that are passed as parameters. Essentially Lambdas can be used all callbacks.

NOTES: All Lambdas must be inside {...}. Unit is the same as the Void return type in Java. done is user defined can be named anything you want e.g. completion, success. success is user defined can be named anything you want e.g. result, status.