Java vs. Kotlin: Which is the Better Option for Android App Development?
The Kotlin mobile app development community is growing. Last year, Google acknowledged Kotlin by making it the second official language of Android app development. Since then, Kotlin has also been steadily building partnerships with Google Cloud Platform, the Spring Framework, and Gradle. Android app developers finally have the pragmatic, modern, and intuitive language they’ve been waiting for.
What is Kotlin?
Kotlin is both an object-oriented (OO) and functional programming (FP) language. It’s compatible with OO and FP styles allowing developers to incorporate elements from each form. Kotlin offers support for higher-order functions, function types and lambdas, making it a great choice for functional programming.
A central focus for Kotlin is to enable mixed-language projects. Kotlin is entirely compatible with Java and Objective C, which makes learning the new language painless. Kotlin introduces improved syntax, as well as concise expressions and abstractions. Using Kotlin with Java eliminates the need for excessive boilerplate code, which is a huge win for Android developers.
Kotlin 2018: A Year In Review
After Google I/O 2017, Kotlin saw explosive growth for Android development. Since then, JetBrains hosted the very first KotlinConf and announced the beta release of Kotlin/Native.
Last year, JetBrains released the first preview of Kotlin/Native, and as we move into 2019, developers now have access to Kotlin/Native Beta. In the Kotlinverse, the adoption of cross-platform development has been slow-moving. Pusher’s State of Kotlin reveals that out of 3,000 respondents, only a quarter have used some kind of cross-platform support with most settling on Kotlin/Native.
Kotlin/Native runs on the LLMV compiler infrastructure allowing developers to write platform-specific modules and compile them directly to native binaries. Kotlin/Native Beta also creates multiplatform capabilities covering all supported platforms and business logic is shareable between components as Android or iOS apps.
In 2018, Airbnb began transitioning away from React Native in favor of Kotlin. In fact, they’ve written 80 percent of their new code in Kotlin. So while the adoption of cross-platform Kotlin development hasn’t been explosive, major industry players are taking note of the many benefits Kotlin has to offer.
Is this the end of Java?
There are mixed opinions from developers.
Java is a reputable programming language with vast open source tools and libraries to help developers. With that said, no language is without fault and even Java is subject to complications that can make a developers job tedious. If anything, Kotlin will introduce solutions to common programming headaches and improve the Java ecosystem as a whole.
In the last year, Kotlin has become a more stable and congruous development option for Android Studio. Some developers seem to believe that Kotlin will oust Java for Android development in the coming years. Other experts see Kotlin and Java existing in parallel without one outweighing the other.
For most, Kotlin’s strengths outweigh the language’s setbacks. There are definite limitations within Java that impede Android API design. Kotlin is inherently lightweight, clean and far less verbose, especially in terms of writing callbacks, data classes, and getters/setters. In other words, Kotlin was specifically designed to improve existing Java models by offering solutions to API design deficiencies.
Kotlin addresses a number of Java-induced weaknesses:
A lot of developers praise Kotlin for being concise. This is a quality Java is not known for; however, readability should always take priority over concision. Yes, the succinct nature of Kotlin uncomplicates a developer’s job and leaves less room for error, but Kotlin doesn’t practice concision for concision’s sake. Boilerplate code is a problem to read through and leads to more bugs and wasted time to try to identify them.
Above is a simple calculator function written in Java. For comparison, here is the same calculator written in Kotlin:
It may not seem like much, but the Kotlin version of this calculator is written in half the lines of code it took to program the function in Java. Brevity is a crucial factor for productivity. Writing large projects becomes easier when a developer is given more power for every line of code. A key observation here is Kotlin does not overlook comprehension for the sake of brevity. The syntax is concise readable and still substantial.
Interoperability is Kotlin’s core purpose. From the beginning, the project’s intention has been to use existing knowledge and expertise to make every library out there available to Kotlin programmers. Developers can simply write modules in Kotlin that work flawlessly within existing Java code. By emitting the Bytecode, a Kotlin compiler allows the two languages to work in unison in the same project.
Inbuilt Null Safety
Kotlin’s type system has inbuilt null safety. The infamous NullPointerException is largely responsible for Android and Java development mistakes. Android relies on null to represent the absence of value, but null can easily destroy an app. Kotlin solves this problem by incorporating inherent null safety. This addition saves developers from writing extra code to work around the issue.
No Raw Types
Before generics came into play, raw types were used quite frequently. Raw types allow for backward compatibility, but raw types can throw a CastClassException and the error will occur during execution and not the compiling stage. Kotlin does not allow raw types, and as a result, produced a more type-safe code.
No Checked Exceptions
The checked exception feature in Java can be problematic. Checked exceptions are when the compiler forces the caller of a function to catch or (re-throw) an exception. The fact is, checked exceptions are often unnecessary and cause empty catch blocks. Non-existent checked exceptions are an annoyance for developers because they’re forced to weed through the code to identify a possible exception that never actually occurred. As a solution, Kotlin removed them entirely, which again minimizes verbosity and improves type-safety.
Is it Necessary for Developers to Learn Kotlin?
Again, there are mixed opinions.
While it is not entirely necessary for developers to make the switch to Kotlin, they’re going to encounter the language eventually. If you’re already familiar with Java, learning Kotlin will be simple. The language is poised to impact app development on a massive scale, so it doesn’t hurt to learn the basics, and growing with modern techniques and development styles will aid the growth of any developer’s skill set.
The Kotlin 2019 Outlook
With colossal brands like Amazon, Pinterest, Netflix, Uber, and Trello all using Kotlin, support from JetBrains and Google, and almost global attachment to the programming language, it’s pretty safe to say that Kotlin will be around for a while. The real question is: will Kotlin become the champion “write once and run everywhere” language?
Kotlin is quickly proving itself to be a superior programming language for mobile app development and presents developers with a wealth of opportunity to experiment with modern programming. Developers can transition between OO, FP, scripting, and declarative paradigms with zero effort, easily setting a new standard for what a programming language should be able to accomplish.
As a full service custom mobile app development company, Clearbridge Mobile handles the entire lifecycle of your product from Planning and Strategy, UX/UI Design, App Development, QA/User Acceptance Testing, to Technical Delivery. We use a unique agile development process that gives you control over scope, reduces your risk, and provides you predictable velocity. Start a conversation today to get started on your mobile project.