How to go from Java to Kotlin

Tim Gray
June 28, 2016

This blog follows my previous blog discussing what programming languages are suitable for writing cloud-focused code using Kotlin. We want to encourage our faithful Java developers (if we had any) to move from Java to Kotlin. So we need to show them how much of an easier time they will have.

Here are a few examples of where Kotlin makes our job easier:


Kotlin has made simple constructors much more concise and easier to read. As an example:
[code language=”java”]public class JavaCode {
   private int age;
   private String fName;
   private String lName;
   public JavaCode(String fName, String lName, int age){
       this.age = age;
       this.fName = fName;
       this.lName = lName;
[code language=”java”]class KotlinCode(private val fName: String,
                private val lName: String,
                private val age: Int){
[/code]At first glance Kotlin is a little confusing, but just like the Java code these variables are private class-wide variables. We can see that this process is much simpler and easier to read than the Java version.
If you want to execute code in the constructor it is as simple as below.
[code language=”java”]init {
   if(age < 0){
       throw Exception("Age cannot be negative")
[/code]What if we want public variables, guess we have to write getters and setters right? Think again.


Properties are one way that we can save a lot of time over coding overhead in Kotlin.
In Kotlin any variable (anything marked with ‘var’) is a property that the getter or setter can be edited after they have been created and used.
[code language=”java”]class KotlinCode(fName: String, lName: String, public val age: Int) {
   var fName = fName //This a public changeable property.
   private var _lName = lName
   var lName: String //This one checks things when it is set.
       get() = _lName
       set(value) {
               throw Exception("Name cannot be blank")
           _lName = value
   var fullName : String = fName + " " + lName
       get() = fName + " " + lName //This one is always the full name and cannot be set.
[/code]As we can see, writing safe public variables is very easy and extensible in Kotlin.

Null Safety

A main selling point of Kotlin is its null safety. Let’s jump straight into an example:
[code language=”java”]var a: String = "abc"
a = null // compilation error
[/code]With the above example, the string a cannot be null. This is the default state for variables in Kotlin. If we need a null in a value than we can append a ? to that values declaration.
[code language=”java”]var b: String? = "abc"
b = null // ok
[/code]This now means that b can be assigned null. This can cause other issues, Kotlin does not like us trying to attempt to use objects that are possibly null. Luckily we have a few tools to help us.
[code language=”java”]val fName : String? = null
fun doAThing(){
   fName.capitalize() //Will not compile as fName could be null
   if(fName == null) return
   fName.capitalize() //Kotlin smart cast system now means Kotlin knows this is not null
   fName?.capitalize() //Kotlin will only call this is fName is not null
[/code]This is just the start of the large set of tools that Kotlin has to deal with nulls.

This is just the start of how Kotlin can help us write better code and more can be found here.
Timothy Gray – Coffee to Code

Tim works on a lot of things here at optimal and writes blogs on a wide range of topics.

Connect with Tim on LinkedIn or read his other blogs here.

Copyright © 2019 OptimalBI LTD.