Image Classification with

This year at their annual developer’s conference, I/O, Google announced that Android now supports Kotlin. For those that don’t know, Kotlin is a cleaner and more expressive and modern language than Java. Our Android team is excited to begin learning and implementing Kotlin. With numerous features and benefits, I thought I’d share the top four features I am excited to dig into.

Nullable types

Touted as the solution to NullPointerExceptions, nullable types in Kotlin make surprise null values in your code much less likely. There is no need for constant null checks as functions and fields on nullable types can be accessed safely with the ? operator.

Reduced to:

Kotlin properties and data classes

With properties in Kotlin, getters and setters are no longer necessary. Getters and setters are created for properties automatically and implicitly called when the property is accessed. This will significantly reduce clutter in data model classes. Additionally, the data keyword in Kotlin can be used to create a data model class with automatic implementations of methods like equals, hashCode, toString, and others.

Reduced to:

Extension methods

In Kotlin, classes can be extended to include custom functionality right where you need it as opposed to creating an entirely new class that extends the one you need custom functionality for. Behind the scenes, Kotlin actually creates static methods for the extension methods where the first parameter is an instance of the extended class.

The milesAfterTrip method used earlier could be added to the Car object as follows:

Lambda functions and Inline functions

Kotlin has better support for functional programming than Java with proper function types.

Whereas the Java code above requires the Predicate<T> interface, the same code in Kotlin could be:

Inline functions eliminate the overhead of lambda functions, which typically cause the creation of an anonymous class and the capture of a closure. Functions marked as inline will simply execute the code contained in the lambda functions that are passed to it at the call sites.

If we make the printOldCars function used earlier inline like so:

Then the result of calling printOldCars:

Is equivalent to

Functionally, this is the same as before, but now the oldCarTester lambda that is passed in does not require additional memory allocations. By the way, the let function used above is also an inline function. You can read more about inline functions here.

These are just a few of the features available in Kotlin. You can explore more Kotlin here.

Interested in an Android app? Or need help maintaining an existing one? Drop us a line. We’d love to chat with you!