“Kotlin, the New Programming Language That Will Make You Stop Using Java”. At the Zebra APPFORUM 2017 Europe conference in Prague, our Android Developer Pieter Otten has inspired others to start using Kotlin. Bummed you missed it? No worries! Pieter will explain to you why he has become a big fan of this new language.
The rise of
Kotlin is a programming language developed by Jetbrains, a company famous for IntelliJ, WebStorm, PhpStorm and their ReSharper plugin for Visual Studio. They were looking for a new programming language that would be better than Java, but still interoperable for migration. Influenced by the languages Java, C#, Scala and Groovy, Jetbrains put a team on developing a new programming language. Kotlin is basically build by the people who experienced the pain of Java.
So what is Kotlin?
Kotlin was unveiled in 2011 and had its 1.0 stable release in February 2016 and 1.1 in March recently. The open-source programming language compiles to the JVM (Java Virtual Machine), Android and JavaScript. This way, Java and Kotlin can be used at the same time (interoperability) on JVM’s and Android devices. It can also run on the front-end using JavaScript. Google officially announced at their I/O conference in May that they now support Kotlin for Android development. Since then the interest, usage and fanbase grew even more.
Compared to Java
To compare Java and Kotlin, one of the examples of the presentation is this Java POJO class and its usage, as seen in the image above. This shows the strength and conciseness of Kotlin, as a simple Person class (with a name, getter/setter and standard POJO methods) is replaced by the keyword “data”. You can also quickly notice the following differences looking at the usage of this Person class:
- Methods in Kotlin are named ‘fun’ (this also makes programming more fun right of the bat).
- If a method doesn’t return anything the return type (in Java: “void”) doesn’t have to be specified.
- Kotlin has type interference, so stating that “name” is of the type “String” isn’t needed. Just use “val”.
- This also makes this variable immutable. If you want to change the “name” variable you should use “var”. This way Kotlin makes you think up front, when writing code, about your variables/fields etc.
- To create a new instance the “new” keyword isn’t needed.
- Kotlin and Java are interoperable. Kotlin is using the Java class Person without any issues.
- The getters and setters from Java code will automatically become properties, meaning “getName()” isn’t available in Kotlin, but the property “name” on Person is
Mediaan adopting Kotlin and its findings
After attending other presentations about Kotlin at Droidcon 2015 & 2016 in London and the GDG DevFest 2016 in Amsterdam, our mobile team decided it was time to look into the new language. After we started using it in October 2016, we quickly fell in love. The first new Android project ended up being 100% written in Kotlin. We haven’t used Java for Android development ever since.
When we look back at our Java experience and how we use Kotlin today, we can conclude the following:
- The code is more concise, it’s just simply less code to write and maintain.
- The code is safer, since it makes you think about (im)mutability, nullability etc. while writing code.
- The above also makes development faster. It’s just easier to do complex things.
- The IDE support is great (we’re using Android Studio).
- The community is big and growing. Existing libraries are supporting or migrating to Kotlin, the internet is packed with blogs, videos, presentations and, of course, StackOverflow questions/answers.
- Kotlin is production ready. We’re doing it and also big companies like Square, Pinterest, Trello and Google.
And now Google has announced it supports Kotlin for Android development, there’s almost no reason to not look into the new language and start using it.
So, how do you start using Kotlin or migrate it?
First of all, there are a lot of online references for the language and learning it:
- Website
- Documentation (tutorials, references, books, etc.)
- Online coding
- Android
- Talks, blogs and presentations on YouTube, Medium and SlideShare
Next to this, the Android Studio plugin also has a quick-conversion shortcut that will convert your current open Java file to Kotlin. This works good. It might need to fix some small things in the code, but it will give you an instant Kotlin version of your Java code for you to look at, compare, and learn. Even more awesome, if you are working in a Kotlin file and you paste a Java code-snippet, it will automatically be converted to Kotlin code when pasted.
For using Kotlin in an existing project, or converting the project, we recommend the following approach:
- Make use of the interoperability (use Kotlin and Java side-by-side).
- Make use of the plugin support for converting files and snippets.
- Write everything new in Kotlin.
- Convert everything you edit from Java to Kotlin.
The future of
Next to JVM, Android and JavaScript support, Kotlin is working hard on supporting more platforms. Support for machine code is the next big thing. Running the code ode on a RaspBerry Pi for example is already possible (in beta). Jetbrains is working hard on adding support for iOS, MacOS and Windows. This will mean that in the future Kotlin might even be the basis of a new cross-platform app solution. More of the future might be announced at KotlinConf, their own conference end of this year in San Francisco.