Clearbridge Mobile > Android  > Kotlin vs. Java: Which is the Better Option for Android App Development?
java vs kotlin

Kotlin vs. Java: Which is the Better Option for Android App Development?

 

The Kotlin mobile app development community is continuing to grow. Back in 2017, Google acknowledged Kotlin by making it the second official language of Android app development. Since then, the programming language has seen a monumental rise in demand in both the developer and enterprise community. With Google announcing that Kotlin is now its preferred language for Android app developers, the language is proving to be a pragmatic, modern, and intuitive programming language.

What is Kotlin?

Kotlin is a statically typed programming language for Java Virtual Machine (JVM) and JavaScript. Described as a general purpose language, Kotlin introduces functional features to support Java interoperability. The Kotlin project was born out of the aspiration for heightened productivity. The goal was to improve the coding experience in a way that was both practical and effective.

 

A central focus for Kotlin is to enable mixed-language projects. Kotlin also introduces improved syntax, as well as concise expressions and abstractions. Using Kotlin with Java reduces excessive boilerplate code, which is a huge win for Android developers.

Kotlin 2019: Where is the language now?  

Since Google I/O 2017, Kotlin has seen explosive growth in Android development and after hosting KotlinConf only twice, the conference has become something of an institution in the developer community.

 

Accessibility on all platforms has always been a primary objective for Kotlin, but multi-platform programming is only the premise to a much more innovative outlook: sharing code between all platforms. With the release of Kotlin 1.3, improvements to Kotlin/Native are advancing the concept of multi-platform convenience. Finally, Android developers can use one integrated development environment (IDE) to develop with Kotlin on all platforms. The newest release makes mobile app scalability more attainable by supporting the invaluable benefit of code reuse, saving time and effort for more challenging tasks.

 

Kotlin/Native uses the compiler technology LLMV to compile Kotlin sources into stand-alone binaries for multiple operating systems and CPU architectures like iOS, Linux, Windows, Mac, and Webassembly.

 

In 2019, more enterprise leaders are migrating to Kotlin or planning to do so. Mobile products like Pinterest, Twitter, Netflix, Uber, AirBnB, Trello, and Evernote are all switching to Kotlin for Android applications. 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.  

 

New call-to-action

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 developer’s job tedious. If anything, Kotlin will introduce solutions to common programming headaches and improve the Java ecosystem as a whole.

 

In two years, 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 coexisting 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 is specifically designed to improve existing Java models by offering solutions to API design deficiencies.

 

Kotlin addresses a number of Java’s weaknesses:

Brevity

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 simplifies a developer’s job and mitigates the risk for error, but Kotlin doesn’t practice concision for concision’s sake. Boilerplate code is a problem to read and leads to more bugs and wasted time trying to identify them.

 

public class ClearBridge { 

public static double calculate (double a, String op, double b) throws Exception { 
	switch (op) { 
		case "add": 
			return a + b; 
		case "subtract":
			return a - b;
		case "multiply":
			return a * b; 
		case "divide": 
			return a / b;
		default: 
			throw new Exception();
		}
	}

}

 

Above is a simple calculator function written in Java. For comparison, here is the same calculator in Kotlin:

 

fun calculate (a: Double, op: String, b: Double): Double { 
	when (op) { 
		"add" -> return a + b
		"subtract" -> return a - b
		"multiply" -> return a * b
		"divide" - > return a / b 
		else -> throw Exception()
	}
}

 

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 in 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

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 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 development mistakes. Android relies on null to represent the absence of a 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, produces 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 annoying for developers because empty catch blocks force developers to weed through the code to identify a nonexistent exception. As a solution, Kotlin removes them entirely, which 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 of the language. Additionally,  growing with modern techniques and development styles will aid the growth of any developer’s skill set.

The Business Benefits of Switching to Kotlin

Redoing large portions of a project in a new programming language is a serious decision; however, Kotlin offers two indispensable business benefits impacting the decision.

Reduced Project Timelines

Kotlin’s brevity benefits mean the language can solve more problems with fewer lines of code. Kotlin’s concise nature allows developers to read and modify code more efficiently which can reduce a project’s time-to-market.

Fewer App Crashes

Again, Kotlin products use fewer lines of code compared to Java programs. Not only does less code reduce development time, but fewer lines of code also support maintenance needs. There is less surface area for bugs to hide and the enhanced readability of the language makes locating bugs easier for developers, resulting in fewer crashes and system failures.

Making the Switch from Java to Kotlin

There are two things to keep in mind when deciding if Kotlin is the right choice for your mobile product. First, make sure your product team is ready to make the switch. Introducing a new language, rewriting and optimizing your project introduces a learning curve. It’s important to make sure your team is ready to adapt to any obstacles in the learning process. It is equally important to acknowledge how the switch will impact your existing architecture. Changing programming languages can cause the deletion, creation and merging of particular artifacts and classes. As a result, your product’s architecture can be damaged. It’s critical to ensure your mobile architecture is expandable and can handle the evolution of your product.

Making major changes to your Android mobile app? We can help you plan for scalability. Ask us how.

The Future of Kotlin

Since the beginning of 2018, roughly 1.5 million developers wrote in Kotlin code and halfway through 2019, that number has nearly doubled. With colossal brands starting to use Kotlin, support from JetBrains and Google, and an 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. Overall, Kotlin is setting a new standard for what a programming language should be able to accomplish.

 

New call-to-action

 

Avatar
Annie Dossey

Digital Marketing Manager