Why Your Company Should Think About Moving from Java to Kotlin
This blog post looks at the reasons your company should think about moving to Kotlin, particularly for web development projects.
Kotlin keeps growing in popularity. It is the language of choice for Android development and is now entering the world of web applications too. Even as it gradually takes Java’s place, Kotlin relies on Java’s mature ecosystem. Designed as a modern alternative, Kotlin tackles many of the shortcomings that stem from Java’s emphasis on backward compatibility – although Java itself has been evolving more quickly in recent years. This blog post looks at the reasons your company should think about moving to Kotlin, particularly for web development projects.
Java’s dominance
Java came into existence nearly 30 years ago. For a long time, this mature language was regarded as the top choice for many complex enterprise computer systems. Java had a reputation as a very stable language, both in reliability and in how slowly it evolved. That stability could be considered a plus, particularly in the enterprise scenarios where Java shines – after all, experimenting with fancy new features in production systems that earn a company millions of dollars is probably not the wisest idea.
Yet in the fast-moving world of software development, even long-established languages have to adapt. Languages that don’t evolve may get left behind. Luckily, the Java community took note and added some nice new features, such as immutable collections in Java 9 , records in Java 16, and pattern matching in Java 21. Despite this progress, Java still trails some of its rivals. What’s more, these new features were built on top of old foundations that don’t really fit them, so they don’t behave quite as they ought to. The outcome is a language that is trying to keep up with the competition but is held back by limitations rooted in some decisions made when it was first designed.
Fine, so Java may be lagging and missing all those fashionable new features – does it matter? Isn’t it just a programmer’s preference, like the color of your shoes – nothing significant? Not at all! From a business standpoint it matters a great deal, and the rest of this article explains why.
Billion-dollar mistake
The billion-dollar mistake is a term coined by Tony Hoare in 2009 [5]. Hoare came to regret inventing the null reference and later acknowledged it was a mistake, since it caused countless errors, crashes and vulnerabilities in software systems. Java’s creators built the very same mistake into their language. This is a fundamental issue for Java, because every reference in the language is nullable by default. It needs to be understood that this is a serious security problem. Research indicates that NullPointerExceptions account for 20-40% of exceptions in production environments. That means a lot of money lost to extra debugging, higher maintenance costs and possible service outages!
There have been attempts to reduce the problem, but every solution in use falls short. In fact, a brilliant new idea for tackling it appears every few years, and it fuels plenty of disputes. Ultimately, there is no clear answer to how nulls should be handled in Java – there are many ways, and none of them is good:
- Assume a given reference will never be null (although it could be).
- Check whether the reference is null before each use.
- Apply a Jakarta @Nonnull-like annotation as documentation.
- Apply a Jakarta @NotNull-like annotation for runtime checks.
- Use Java 8’s Optional type.
- Cross my fingers and wait for Null-Restricted and Nullable Types.
- Invent one more approach and make this list even longer.
The first one naturally leads to many NullPointerException bugs, because such assumptions rarely hold. The second fills the code with countless if statements, which hurts readability and maintainability and, in some situations, may even introduce race conditions.
The third and fourth look better, but sprinkling these annotations all over the code is far from perfect. Developers frequently forget them, and those who remember tend to put them only on public interfaces or package facades. Internal code is often left out, so the risk of NullPointerException remains. Moreover, the third option is purely a documentation aid – if you pass a null as a @Nonnull parameter, all the IDE can do is display a warning, and only if it is clever enough. The fourth one looks better, since it throws an exception when null safety is violated. Still, it is only a runtime check, so its main role is to stop the problem from spreading by catching it sooner.
The fifth approach entered the Java world from functional programming. It is simply a type wrapping a reference, meant to state explicitly that an object may sometimes be empty (a different word for null). So it doesn’t make the programmer do anything at the syntax level – it’s just one more assumption.
The sixth approach looks more promising still. It brings syntax-level control over the nullability of reference types via ! and ? markers, much like Kotlin does (more on that in the next section). It is currently a preview feature, but its main limitation is that it won’t change existing library code – so, once implemented, it would cover only part of our codebase.
In short, yes, Java offers null-safety tools, but none is perfect, and there is no single standard way. Well, strictly speaking, there is one standard way: treat nulls as part of Java, since the language was built around them. So what would really solve it? One possibility would be to remove implicitly nullable reference types from the language syntax altogether. That, however, would mean introducing breaking changes to Java, which is practically impossible.
How Kotlin deals with nulls
A major strength of Kotlin is that it put language safety first from day one. Having learned from other languages’ mistakes, Kotlin kept the billion-dollar mistake out of its design. Kotlin chose a simpler and more elegant way of handling nullability:
- All references in Kotlin are non-nullable by default.
- Developers have to declare nullable references explicitly by appending a
?(question mark) to the reference type. - Before a reference of a nullable type is dereferenced, a null check has to be done.
This design pays off because:
- Standard (non-null) references don’t need nullability checks at all.
- When nullability is needed, developers consciously opt in by appending the question mark symbol (
?) to the type. - Referencing a null in place of an object is possible only in a very restricted way – the programmer has to use the special (
!!) syntax, which is discouraged.
This approach is more elegant, simpler and safer than anything Java offers. This design decision has protected many Kotlin applications from NullPointerException, making the language safer and applications sturdier. Consequently, Kotlin has become a great option for enterprise applications.
Immutability
Immutable collections play a central role in the overall safety of a language. Most of the time, collections passed to or returned from another part of the system aren’t meant to be changed. Letting them be changed can break encapsulation and also causes unexpected state changes that the original author’s code didn’t account for. That can result in many bugs that are hard to track down.
Java 9 did add support for immutable collections, but it did so through simple wrappers around ordinary mutable collections that throw an exception on write operations. That may be enough to ensure encapsulation and avoid unexpected state changes, but it creates a new problem by violating the Liskov Substitution Principle. Put differently, this approach introduces types (immutable collections) that don’t correctly implement the methods (write operations) of their parent type (mutable collections). This mismatch can cause more errors and confusion. Just like with null safety, this imperfect (or even ‘dirty’) approach was chosen so that the safety feature could be added without breaking changes.
Kotlin supports immutable collections out of the box. It achieves this by properly redesigning the collection hierarchy so that mutable types inherit from immutable ones, which keeps the Liskov Substitution Principle intact. Moreover, collections, like other things in Kotlin, are immutable by default. Usually, you need to add an extra word (mutable) to work with mutable collections. This further nudges developers away from mutable collections, which most of the time aren’t necessary. It makes code safer by preventing accidental data changes, which ultimately cuts debugging time and maintenance costs for businesses.
Kotlin’s syntax gets more done
Kotlin is known for its concise syntax and many built-in features, which frequently produce code 20-40% shorter than the Java equivalent. This conciseness makes the language easier to write and reason about. It also allows some kinds of errors to be caught earlier. What’s more, Kotlin programmers can deliver the same functionality considerably faster than Java developers. Some companies report development speed gains of up to 40%, but that number is probably on the optimistic side; a more realistic estimate is somewhere between 10% and 30%.
Kotlin is also well known for its built-in functions inspired by functional languages (e.g. fold, reduce, associate, groupBy, zipWithNext). They let developers achieve a lot in just a few lines, which effectively speeds up software implementation. Most of them are available in Java or in libraries such as Guava, but they are definitely not as concise or as pleasant to use as in Kotlin, which somewhat puts programmers off using them.
Although Kotlin is more concise than Java, its syntax is a bit more complex and demands some knowledge of new concepts. At first sight the code looks clean, but figuring out what each part really does takes effort. Lambda receivers and extension functions may look rather exotic to Java developers. These features are certainly excellent for building DSLs (just see what Gradle does with them), but at first they can make your code feel like a puzzle. Conciseness may be tempting, but overly compact syntax can harm readability – it’s best to steer clear of one-line hacks to keep code clear and understandable. The upside? Once the initial confusion passes, Kotlin’s shortcuts and richer standard library will help you ship code more quickly.
Functional programming
The IT world is slowly shifting towards functional programming (FP) and away from a purely object-oriented (OO) approach. The FP paradigm brings immutability, freedom from side effects and predictability, which makes code safer, easier to reason about and better suited to concurrent execution. Purely OO applications, particularly those that lean heavily on inheritance to extend functionality, tend to grow complex and hard to maintain over time. Then again, building systems entirely in the FP paradigm can be harder, especially as most developers were trained mainly in the OOP style. That makes a hybrid approach, taking the best of both paradigms, more appealing. Kotlin hits the sweet spot in this trend, with syntax that lets developers write FP-style code in an OOP setting, or stay with pure OOP where FP doesn’t fit.
Kotlin offers both OOP and FP constructs, so you can combine the two styles or pick one if you prefer. Function types, extension functions, immutability and null-safety make Kotlin a great fit for FP-style programming. You can model a function’s success or failure with sealed classes and pattern matching, or simply use exceptions if you want (though most of the time it’s better not to). You can build a function from a chain of functions, or use loops, ifs and variables if that makes the code clearer (but it probably won’t). You can use higher-order functions, or stay with strategy/command/template method patterns (though your teammates may not be delighted). The point is that Kotlin gives you choices, which can also be a curse, since your team has to agree on which approach to follow.
Let’s face it – Java isn’t sexy nowadays. Companies are turning to functional programming not only for its technical advantages; it also helps bring talented developers on board. Those developers deliver real business value through quicker delivery, higher code quality and more innovative solutions. For instance, when Amazon Prime adopted Kotlin, it saw both greater developer satisfaction and better productivity.
Kotlin works with Java
Kotlin runs on the Java Virtual Machine (JVM), so you can use a broad range of Java tools and libraries, Spring included. It also means Kotlin files and Java classes can live side by side in one project. This makes the move to Kotlin easier for many projects and teams. JetBrains also offers IDE tooling for converting Java code to Kotlin, so Kotlin programmers can still easily paste code snippets from StackOverflow into their projects 😉. The automatic conversion does need some manual polishing, particularly to make full use of Kotlin features such as its nullability handling, but it gives a good starting point for migration. The key thing is that introducing Kotlin to your Java team isn’t a revolution: the ecosystem stays the same, and your team can keep using all the existing tools, so the transition is both safe and smooth.
Just how popular is Kotlin?
Most companies have already moved their Android projects from Java to Kotlin, mainly for the reasons given above, but also because Google strongly backed this shift. Kotlin is a great choice for backend web application development too, yet it hasn’t gained as much ground there. Nevertheless, case studies from companies such as Amazon, Zalando, DoorDash and Revolut show that Kotlin is an excellent option for backend development as well.
Judging Kotlin’s current popularity from job market data is hard, since Kotlin positions are usually combined with Java. In practice, many Kotlin backend services are built by developers who used to work with Java. That’s good news, because if your company picks Kotlin, it can still draw on one of the biggest pools of software engineers on the job market. These Java developers would, however, probably need some training before going deep into Kotlin, particularly if they have no functional programming experience. Fortunately, most good Java developers do have that kind of experience with the FP paradigm.
Why not?
Kotlin has a number of advantages, but like every programming language, it has its downsides too. Its main business-related drawback is that it’s less well known than Java, particularly in web development. Kotlin is popular mostly in mobile applications. Treating mobile as Kotlin’s only territory is a mistake, though. Still – being less popular means fewer programmers available on the job market. It also raises the question of the language’s long-term viability – is it safe to pick a less widely used language for projects that must be maintained for decades? That said, Kotlin is backed by JetBrains and Google, so nobody expects it to be suddenly abandoned – at least not because of JetBrains 😉
Technically, Kotlin is a great but not flawless language, as a few features are missing. Kotlin has pattern matching, but not a fully-fledged one – pattern matching doesn’t support destructuring or complex expressions. Even Java, with the syntax introduced in Java 21, offers more advanced pattern matching. Secondly, Kotlin’s default accessor is public – meaning every class, function or property is public unless stated otherwise – which can encourage bad practices, since access should be restricted by default and opened only when necessary. Kotlin also lacks anything like Java’s package-private, so Kotlin’s syntax doesn’t support building public facades that hide a package’s internals. There are a few other minor shortcomings too, but they deserve a separate blog post – stay tuned, we plan to publish it later. The bottom line is that we don’t see Kotlin as a perfect language (no language is perfect), but it is a very good one, probably among the best choices for many use cases.
Conclusion
Kotlin has a number of advantages over Java, such as stronger language safety, shorter development time and a better fit with modern programming paradigms. Moving from Java to Kotlin doesn’t demand extensive retraining of the team, because Kotlin is still part of the Java ecosystem. All this makes Kotlin an attractive option both for new projects and for teams that want to modernize their existing Java codebases. Nevertheless, Kotlin being less mature than Java brings some risk, especially when the team has no trained Kotlin developers or intends to rely on Kotlin-specific tools that haven’t been battle-tested yet instead of established Java libraries.
At our company, our experience tells us that Kotlin is mature enough to be used successfully even in large backend projects. We know how much choosing the right tools affects a business. We are also well known for our Scala expertise – Scala being another JVM-based language that offers many of the benefits described in this article.
So, will you stick with Java and risk a billion-dollar mistake? 😉
🎵Build a safer future – with Kotlin! 🎵