Java is one of the most popular programming languages for server-side application development in the world. It is used to power international corporations in financial, healthcare, governmental, and other sectors. However, since the creation of this language, many things have changed. This article tells about my experience and of using Kotlin after Java development.
For many years the Java ecosystem has grown dramatically. Many tools like maven, Gradle, and leningen were created to assemble and pack classes into projects.
Also, people did not like some of the features of Java. That is why they started to invent programming languages based on JVM. Java Virtual Machine gives the computer the power to run Java programs or the ones which are compiled to bytecode. Especially this is the biggest asset of the Java ecosystem.
Nowadays, almost all main languages are having some kind of JVM implementation. However, several standalone high-profile languages were created to fix Java mistakes. There are Groovy, Scala, Clojure, and Kotlin.
Clojure is pretending to be one of the most hipsters among them. Scala was done in a university environment and complex distributed systems are written well in it. Companies like Zalando and Spotify are actively using it. Kotlin on Android has first-class support from Google.
Kotlin was invented by JetBrains who are developing the best IDE for Java called Intellij Idea. The first stable release was issued in 2016. The Developers Community liked it because of its simplicity and interoperability with Java.
I recently worked in a company where the backend was written mostly with Kotlin which has some parts in Java. In this article, I will summarize the advantages of the language for the process of software development in the Java-first team.
The first glance at Kotlin gives a very interesting insight. It is very verbose and shortcode for making various things. For example, I took the random ‘Program to Find Roots of a Quadratic Equation’. It takes 30 lines to make this happen in Java. I have pasted it into IntelliJ Idea which suggested converting to Kotlin. With small manipulations and simplifications, it takes now 21 lines of Kotlin which also became shorter. The code is situated below.
As you can see, there is no need to declare type because the language will figure it out without the developer’s participation. The functions declaration is short and nice also with ‘public modifier as default. There are no annoying semicolons also! The final keyword is now replaced with just `val`. You won’t need to say explicitly that the function returns void.
One more great feature of Kotlin is that it has great capabilities to write not only object-oriented code but a functional one as well. There are plenty of built-in operators for working with lists or maps.
Data class is the next big relief after using Java for years. It is a way to create entity classes but without a need to write getters/setters or installing Lombok. Every field is one line of code that can be directly accessed after the object of class Person is created. The basic functions of the Object can be overwritten in a way similar to Java one. Functions can be declared with = instead of curly brackets to simplify your life and without a need to write the type explicitly.
Interoperability with Java
In the case of the company I worked for, the software development process started with Java. Kotlin needed to be a JVM language. It was created to replace Java in Android. However, looks like it is working great for just the other purposes too. In the codebase, we still had parts written in Java but the percentage of lines of code was decreasing because new features are written in Kotlin.
Java ecosystem is rich in libraries that are used for various purposes. For example, we were using Java persistence API without any problems in Kotlin. All the time Kotlin makes the experience of using a library from another language more delightful because it will understand how to write it in the own way.
One can find the code for Kotlin in GitHub repository which is an open-source project. This is great news after years of different cases related to the change of Java owners and the lawsuit between Oracle and Google. I quickly checked the locations of the profiles in the ‘Pull Requests’ section. People from Denmark, the USA, Russia, and Japan are actively contributing to the Kotlin language itself. Many other frameworks and tools on top of that are not even counted.
I think that community-driven development is giving a lot to the language. Kotlin came from the industry which means that real people are solving everyday tasks with this language. It means that they are improving the language according to their needs which is much better than a lock to a specific corporation. Now individual contributors, Google, JetBrains, and many other companies are developing language.
Future of Kotlin
The Kotlin is claimed to be a multiplatform language. It means that in one language one can write for backend, frontend, and native using just one language. Also, it means that some parts are reusable on the platforms, mentioned previously. It is a similar idea like React uses for their React Native. It has pros and cons but many apps were written with this stack.
One other interesting project from Kotlin’s ecosystem is ‘Kotless’. It is a full framework to create static websites, APIs, and deployments to AWS in one package. It has a feature to create Terraform code and to run a cloud environment locally for development and debugging.
All in all, Kotlin is the logical development of Java which gives superpowers to the teams after its adoption. It is the easiest language to switch for if saving JVM-based toolsets and deployments is business-critical.