Learning Kotlin as a Java Developer

Introduction

Andy Aspell Clark
Version 1
5 min readJul 5, 2022

--

This post is going to detail what I learned while learning Kotlin.

I have been using Java (on and off) since the late 1990s (JDK 1.1, using AWT for GUIs). I remember the transition from AWT to Swing and how hard it was to get my head around the different ways of doing things. Getting my mind to think in the different patterns required for Swing took a lot of effort.

Since then, I have used several programming languages and GUI frameworks, and with that experience, I was hoping the transition to Kotlin would be easier than the transition from AWT to Swing.

Why learn Kotlin?

One question I asked myself was “Why learn Kotlin?”. Keeping up with changes to the Java ecosystem (frameworks, libraries, build tools, cloud etc…) takes quite a bit of time and effort. Will learning Kotlin just give me more to keep up to date with? Well, it probably will give me a bit more to keep up to date with, but as Kotlin runs on the JVM and has been designed to inter-operate with Java, the same frameworks, libraries etc. can all be used with Kotlin, so I will only be learning the language, not a whole new ecosystem.

One of the biggest reasons for me to learn Kotlin is that Google seems to be transitioning Android development away from Java to Kotlin.

Where to start?

I was pointed towards a couple of courses to get me started learning Kotlin:

  1. LinkedIn — Kotlin Essential Training
  2. Coursera — Kotlin for Java Developers

and there are the Android development website and the Kotlin language website:

  1. Android developer site
  2. Kotlin Webpage

I first tried the LinkedIn course as it is only about 3 hours long.

  • The course is a good introduction to Kotlin, but (as is to be expected from a 3-hour course) doesn’t go into much detail about any of the features touched upon.
  • The course is structured well and each section has a quick challenge to test how well you’ve taken in some of the concepts presented.
  • It does cover all of the basics, but if you already know Java then most of the content is quite easy to pick up.

After I had completed the LinkedIn course I took the LinkedIn Kotlin skills assessment and scored “Higher than 63%” of assessment takers. Unfortunately, that means I did not get a LinkedIn skills badge as I would need to beat another 7 or 8 percent of the assessment takers.

This showed me that while the LinkedIn course is a good introduction, it does not cover Kotlin in enough detail to allow you to go on to developing Kotlin after just that course.

There are many ways to continue learning Kotlin, but I was guided towards the Coursera course next.

The Coursera course is written by JetBrains (the creators of Kotlin) and as such, is a good way to get into Kotlin development. It is a 5-week course and as such requires a bit more commitment than the LinkedIn course, but it does go into much more detail on all areas of Kotlin. The Coursera course is free, but if you want a certificate, then you will need to pay for it.

How to learn?

I do not learn that well by just reading a book or watching some videos, I need to put that learning into practice as soon as possible to get my mind to remember it.

Both Kotlin courses were good in that they had practical elements to them which get you to practice the features being taught. This helps cement the knowledge and shows how the feature is used in code, but the samples are very small snippets and don’t show how the code integrates with other frameworks/libraries.

Programming Languages on their own are very limited. Knowing just a language won’t get you very far (unless you are working on a bare metal boot loader, after that you have libraries available to use).

To see Kotlin in a real-world application I decided to write a simple Spring Boot CRUD API for retrieving journalling prompts. (See this BBC article for the importance of journaling.)

(This article isn’t going to go through the code for that app. You can view the code on GitHub if you are interested.)

The Spring Framework has supported Kotlin for a couple of years now and the Spring reference documentation includes sample code in both Java and Kotlin.

I created a new Spring Boot project in IntelliJ using the Spring Initializer which allows you to choose between Java, Kotlin and Groovy as the development language. you can access the Spring Initializer independently by navigating to the spring initializr webpage.

As this was a simple CRUD project with no real business logic it was quite simple to write but there were a few issues that started when I was creating the project.

The first is that I am used to using lombok to lower the amount of boilerplate code, so I automatically chose that as one of the libraries in the initialiser and started using it for logging etc. This was fine, but when I needed to create an entity class I automatically use the @Data lombok annotation. This was allowed in the IDE, but I could not get it to work.

I started googling for using lombok annotations in Kotlin and kept seeing links that were comparing Kotlin and lombok. This made me realise that I should use Kotlin features instead of lombok annotations. This led me to replacing the lombok @Data annotation with Kotlin data classes. Once I realized I should use data classes instead lombok annotations I decided to remove the lombok library completely.

I also ran into some weird problems that I could find no workaround for. These turned out to be related to the directory structure used for my Spring Boot/Kotlin/Maven project. the source code had automatically been created in src/main/Java and while the code compiled successfully, I was getting some unexpected exceptions when I tried to run the project. It took me a while
to think about moving my class files into a src/main/kotlin directory, but when I did the code compiled successfully.

Writing applications in Kotlin is very easy for a Java developer. In fact you can continue to use Kotlin in the same way as you use Java, but this would not be utilizing the full power of the Kotlin language.

Writing applications in Kotlin is more than just the syntax differences from Java. You need to remember to use the features that Kotlin gives you and if you are mixing languages, then that could be quite difficult. It is similar to learning C++ if you already know C. You can write C++ programs in a C style, but you’ll be missing out on some of the features that C++ brings with it.

Conclusion

When learning any new language, it can be difficult to put aside the concepts that you have already learned. Getting into the new languages “mindset” is probably the hardest part. This is especially true when learning a programming language that uses a different paradigm (e.g Constraint Programming Languages such as Forth, Postscript, etc…).

Learning new languages can be a challenge, but it can also prove beneficial even if you don’t end up using the language as it will help you keep an open mind to using programming languages in different ways.

About the Author:
Andy Aspell Clark is a Java Technical Lead here at Version 1.

--

--