A Complete Kotlin Guide For Java Developers [2020 EDITION]
Aug 207 min read
Senior Frontend Developer at Ideamotive.
Have you been toying with the idea of switching to Kotlin but haven’t made up your mind yet? After reading this post, you may come to a decision faster.
If this blog post were published a few years ago, most Android devs wouldn’t probably even bother to skim through. As the native language for Google’s mobile OS, Java was holding a strong position on the mobile development ground, and very few people saw it being replaced by another technology any time soon.
That is until the D-Day came. First, in 2017, Google dubbed Kotlin ‘the official Android language’ side by side Java. Then, following the language’s dynamic expansion, in 2019, the Mountain View giant went all the way to announce that Android development is heading the Kotlin-first path.
As a result of Kotlin’s transition from a seasonal hype into a broadly-adopted industry standard, more and more developers are carefully weighing their options, considering a shift from Java. If you are one of them, our guide will hopefully help you make up your mind faster and step into Kotlin development with confidence.
Kotlin Is Becoming a Dominant Mobile Development Technology
Google’s steadfast support for Kotlin inspired a sharp rise in the language’s popularity over the last two-three years. In StackOverflow’s 2020 Developer Survey, Kotlin ranks as the fourth most admired programming language. Nearly two-thirds of all programmers who have worked with it before are willing to continue to use the technology. Additionally, over 12% of all developers who don’t use Kotlin at the moment want to learn it.
According to the official site, “Kotlin is a modern statically typed programming language that will boost your productivity and increase your developer happiness.” Indeed, the language was created by developers for developers as a response to Java’s deficiencies in the context of Android development.
Kotlin: Quick Facts
First launched in 2011 as an internal project by JetBrains, and published as open-source a year later. In 2017, Google announced its first-class support on Android. On 7 May 2019, Kotlin became the preferred language for Android development.
It is a cross-platform, statically typed, general-purpose programming language with type inference.
Seen as an upgrade to Java for mobile apps development, Kotlin has introduced string templates, data classes, null-safety, extension properties, coroutines, and other much-needed modern features. All of these enhancements are to simplify programming and make the code shorter, more readable, and error-proof. But what exactly makes Kotlin a viable alternative for Java developers?
Kotlin Boosts Your Productivity
Being a concise and expressive language, Kotlin allows you to achieve more while doing less.
First of all, the language follows the principle that the data model should be simple and intuitive. To that end, it embeds getters and setters into properties.
It also makes use of data classes to contain object states and supply auto-generated equals, hashCode, toString, and copy method implementations.
Smart casts, on the other hand, get rid of explicit casting on variables, automatically converting a variable to an expected type.
Another highly useful feature of Kotlin that streamlines development workflows is type inference, which allows the compiler to infer the variable type from the context.
Kotlin makes heavy use of predefined operators that can replace common methods and functions for greater code conciseness and readability. Here’s an example of overloading the unary minus operator:
All of these inherent Kotlin features mean that the language is as powerful and versatile as Java but solves problems much more efficiently. By using fewer lines of code, Kotlin boosts developers’ productivity and helps eliminate repetitive tasks that usually drive programmers up the wall.
Kotlin Doesn’t Force You to Quit Java
The whole idea behind Kotlin was to support mobile devs with features they lacked but without risking that the already existing code would go to waste. For that reason, Kotlin is 100% interoperable with Java, as well as all its libraries, frameworks, and the JVM.
This means that as a Java developer, you don’t need to abandon your favorite language and tools. You can still use Kotlin alongside Java in your applications, without migrating the source code. To do that, convert Java files to Kotlin files with .kt extension.
86% of Kotlin programmers still use Java in their work
The compact, concise, and readable Kotlin code results in fewer errors. Additionally, Kotlin’s sensitive compiler spots and flags common programming mistakes. More importantly, however, Kotlin helps developers eliminate the dreaded Null Pointers from the code.
Kotlin’s type system distinguishes between nullable references that can hold null and non-null references, which cannot. NPEs are allowed only under specific circumstances, for instance, for Java interoperation.
To be declared as nullable, a type needs a question mark.
The lack of a question mark denotes that a type is a non-null reference:
Kotlin Supports Asynchronous Programming
Kotlin’s standard library provides a low-level API to allow other libraries to take advantage of coroutines. These light-weight cooperatively multitasked threads enable asynchronous programming, which positively impacts app performance and responsiveness. They also simplify and streamline tasks such as network calls and DB updates.
Through coroutine support, Kotlin helps resolve issues with long-running tasks and suspend performance-heavy functions running on the main thread. These capabilities are much-desired, especially in complex, multi-threaded applications (e.g., gaming apps) and in client software with expressive, responsive UI.
Kotlin Is Easy to Learn
One of the top Kotlin advantages is the fact that it is easy to learn, especially for Java developers. The technology is strictly programmer-oriented, and it focuses on resolving key development challenges that keep things simple for you and other devs. Kotlin has a fairly straightforward, logical syntax that leads to brief, highly readable code. It also enjoys strong business and community support, offers an extensive library of learning resources, and costs nothing to learn and use.
What Makes Kotlin Stand Out From the Crowd?
We’ve already touched upon several distinctive Kotlin features as a great candidate for mobile app development projects. Let’s add some more characteristics that make it worth your time:
In Kotlin, a variable can only be assigned to objects of one fixed type, the type of the variable, allowing for error detection during the compilation, before the code is executed, e.g.:
Kotlin supports non-local jumps, which allow you to exit the function anywhere.
This feature speeds up working with collections.
Extension functions allow you to extend existing components without writing methods inside them, for cleaner code.
These functions operate on other functions by taking them as arguments or by returning them. And the result? Simplified coding!
You can decrease load times by loading only essential resources into the main memory.
Unsafe cast support
The cast operator throws an exception if the cast cannot be executed.
Who Uses Kotlin?
According to Google, over 60% of apps available at Play Store use Kotlin. Yet even though Kotlin is predominantly used for building Android apps, it’s an incredibly versatile language that also supports the development of iOS, backend, and web applications. You don’t need to take our word for granted, just consider the following stats:
Two out of three Kotlin apps are for mobile devices
And who, in particular, uses Kotlin to develop these web and mobile apps? Let’s take a look at some examples.
Kotlin for Mobile Development
We don’t know what is more awesome, the fact that Kotlin powers a majority of Android apps, or that it can also be used to build production-ready iOS mobile applications. Native Kotlin makes cross-platform development a reality for the language, drastically reducing the time needed to ship interactive, stable products to the world’s largest mobile app marketplaces.
Notable examples of companies that leverage the interoperability of Kotlin in their cross-platform mobile apps include Target, Quizlet, CashApp, Yandex, and VMWare. Top names for Android mobile app development feature Pinterest, Uber, Basecamp, Evernote, Trello, and Kickstarter.
With more than 1.5 million downloads, Evernote app uses Kotlin for high reliability and performance
The second most popular application of Kotlin is on the web backend. The language comes with a variety of frameworks for server-side development (you will find them here), and it benefits from complete interoperability with Java, which means it can also leverage its tooling. Kotlin for Web Development
One of the most popular frameworks to serve the purpose is LibKTX, a Kotlin extension to a Java-based, open source cross-platform game engine maintained by BadLogicGames. Unreal Engine, Unity, jMonkey can all be used with Kotlin to create interactive, high-performing mobile games.
TetroCrate 3D - an example of a multi-platform game app built with Kotlin
Kotlin for Machine Learning and Data Science
Concise, flexible, and easy to learn, Kotlin makes a pretty good fit for ML and data science applications. As a language, Kotlin enables quick turnaround and iterations, which are essential for experiment-heavy data science projects. It also integrates with Jupyter Notebook and Apache Zeppelin notebooks, which facilitate data visualization and exploratory research, and offers an expanding set of own data science libraries. Additionally, Kotlin can leverage a host of machine learning and mathematical JVM libraries such as Krangl, ND4J, Smile, CoreNLP, and many others.
Kotlin is Java-based at its core, and it can run on any Java environment. Hence, for a proficient Java developer like yourself, adding it to your portfolio won’t be too much of a hassle. Think of transitioning from Java as upgrading from a worn-out but dependable Toyota to a brand-new, ultra-fast, and reliable Porsche. The skills you need to handle both vehicles are similar but the upgrade will give you a massive experience boost!
You will find Kotlin’s syntax and structure highly familiar to what you already know. And to master Kotlin-specific notions, such as null safety, smart casts, extensions, or coroutines, you can take advantage of an abundance of tools and tutorials that will get you into a fully-proficient Kotlin developer in no time: