A rudimentary overview of Kotlin

Igor Ganapolsky
4 min readMay 24, 2017

--

Like many Android developers, I was super excited about Google’s announcement of Kotlin at IO 2017. It meant that a language that was lurking in the shadows of Android development is now at the forefront. However, like many other developers, I was a bit skeptical about jumping on the Kotlin bandwagon. Primarily because it would mean giving up my Java skills to learn a completely new language.

Nevertheless, I believe it is the future of Android development, so it is of utmost importance to catch up. With that said, I decided to write a short overview of Kotlin to get started.

Kotlin is a statically-typed language that can compile to Java bytecode. It is extremely similar to Swift:

Kotlin was inspired by the best of Java, C#, JavaScript. Functions in Kotlin are first class citizens. It has full Java interoperability, meaning it was specifically designed as a replacement for Java. That means your Kotlin classes can inherit from Java classes, implement Java interfaces, call Java methods, and more. Contrarily, Java code can inherit from Kotlin classes, implement Kotlin interfaces and call Kotlin methods.

Android Studio can convert Java code to Kotlin:

Before Kotlin conversion
After Kotlin conversion

Android Studio can also show bytecode generated by Kotlin:

Debugging Kotlin works just like debugging Java code. And JNI is fully supported with Kotlin, simply mark a JNI method with the external modifier:

external fun foo(x: Int): Double

Now lets illustrate some features of Kotlin

A) Extension Functions

Like in C#, in Kotlin you can define methods on a class and not have to write them inside the class:

class Car {  var make = ...  var model = ...}// The original creator of the Car class omitted this methodfun Car.setIdentification(make: String, model: String) {  this.make = make  this.model = model}

This is handy if you’re working with library code that is not changeable. An extension function does not access private fields of the class it was declared on, and it cannot violate the invariants that the class was authored with.

B) Non-nullable types by default

This means you never have to worry about NullPointerException!

· ?. (safe navigation operator) can be used to safely access a method or property of a possibly null object. If the object is null, the method will not be called and the expression evaluates to null.

// returns null if foo is null, or bar() is null, or baz() is nullfoo ?. bar() ?. baz()

?: (null coalescing operator) often referred to as the Elvis operator

fun sayHello(maybe : String?) {  // use of elvis operator  val name : String = maybe ?: "stranger"  println("Hello $name")}

C) Getters/Setters from Java are automatically transformed into properties

To use a property, we simply refer to it by name, as if it were a field in Java.

fun copyAddress(address: Address): Address {  val result = Address() // there's no 'new' keyword in Kotlin  result.name = address.name // accessors are called  result.street = address.street  return result}

D) When instead of Switch

when (x) {  1 -> print("x == 1") 
2 -> print("x == 2")
else -> {
// Note the block - it's like default
print("x is neither 1 nor 2")
}
}

E) Single-expression functions

When a function returns a single expression, the curly braces can be omitted and the body is specified like this:

fun double(x: Int) = x * 2

F) Sealed Classes

Similar to Unions in C or Enums in Java. They are useful for state machines , when a value can have one of the types from a limited set, but cannot have any other type.

sealed class BluetoothState {  class Connecting() : BluetoothState()  class Connected(val ssid: ServiceIdentifier) : BluetoothState()  class Disconnected() : BluetoothState()}

G) Name arguments, Default arguments

Function parameters can be named when calling functions. This is useful when a function has many parameters or default ones.

fun createCar(make: String,  manualTransmission: Boolean = true,  convertible: Boolean = true,  leatherSeats: Boolean = false,  model: String = "Super Turbo Speedster") {}

we can call this using default arguments:

createCar("Toyota")

In Conclusion

There are many more features to Kotlin, and the links below are great starting points.

How do I choose between the Java and Kotlin languages?

You don’t have to pick! You can use both together as you see fit.“ ~Google

Works Cited

http://frozenfractal.com/blog/2017/2/10/10-cool-things-about-kotlin/

http://kotlinlang.org

https://developer.android.com/kotlin/faq.html

--

--