Kotlin is a JVM language that hit version 1.0 about a year ago (February 2016).
It is developed by JetBrains
, the same people who make my favorite suite of
IDEs. The language itself is open-source under the Apache License
2.0 and is
developed as a community project over at kotlinlang.org
. Kotlin is something
that I have become rather excited about over the past year. This post’s goal is
not to teach you Kotlin but to get you excited about it!
Goals of Kotlin
The stated goals of Kotlin are: Interoperability, safety, and tooling.
Kotlin is designed to be 100% interoperable with Java. There is always the
ability to use Java libraries from Kotlin, just as there is the option to use
Kotlin libraries from Java. It is even extremely easy to mix Java and kotlin
within the same codebase without a problem. See the documentation
more info regarding interoperability
Type safety is important to the design of Kotlin as well. It makes an attempt to
catch type errors at compile time in order to avoid runtime instability.
While Scala has many of the features that the Kotlin team wanted in a language,
it took significantly longer to compile and had significant runtime
dependencies. Kotlin has a stated goal of being as fast to compile as Java and
does not have a runtime dependency (though it does need its standard library,
There are official Kotlin plugins/support for IntelliJ IDEA (community and
ultimate), Android Studio, Gradle, Maven, Ant, even Eclipse.
In Kotlin the type comes after the name. If you use Typescript, Go, Swift, etc
you’ll be familiar with this concept.
is non-final, but a
Functions are not required to belong to a class, and are defined with a
Where can I use Kotlin?
You can use Kotlin (nearly) anywhere you are already using Java. It can be added
into a backend project relying on Spring or Play frameworks with ease. It is
also gaining huge popularity in the Android development community.
Kotlin on Android is what drew me to the language in the first place. I, along
with much of the Android development community, had a growing desire for a less
verbose, more concise, more modern language to build our applications with. The
fact that Android Studio fully supports Kotlin out of the box is a huge help to
knocking down the barriers of adding Kotlin into an existing codebase, as well
as making it easy to start new projects with Kotlin.
Now for the fun part: showing off the features of Kotlin. My favorite way to
introduce Kotlin is not to answer “what can I do with Kotlin?”, but instead
answer “what don’t
I need to do in Kotlin?”.
What don’t you need to do in Kotlin?
Pretend you need to make a lightweight model class to represent a person. For
simplicity’s sake, a person can have a name, age, and a city. You want to be
able to compare two person objects (
) as well as print
out the model for logging or debugging reasons (
In Java this lightweight model looks something like this:
We have 40 lines of code to achieve this simple and common task. Kotlin can do
all of this using its
functionality… in one line.
A data class has final fields defined after the class name, it generates
(internally) the constructor,
(not used in my example), and
A value must be assigned either at the time of declaration or during instance
creation. If not, it will throw a compiler error
To allow for a value to potentially be null, you must use the
the type. If you have a potentially null type, you must safe-call (
assert that you know more than the compiler (
Other nifty features
Single value functions
Lambdas and iterators
To me, this is quite similar to “monkey-patching” in Ruby. Officially, it draws
inspiration from C# and Gosu.
Here is a simple example of adding a
There is so much more…
This is hardly an exhaustive showcase of Kotlin’s features. I hope that it was
able to get you excited or thinking about trying something new on a project
though! If you’d like a relatively safe way to get started, try dropping kotlin
into an existing project and using it to write some tests. Your tests will
become shorter, more clear, and you’ll hopefully want to use Kotlin everywhere!
I’m intending to use Kotlin any time I have the opportunity. I have high hopes
for this young language that shows lots of promise, hopefully you’ll join in on the