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 method fun 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 null foo ?. 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