Java vs. Kotlin: Improving Android Development

Posted by ongraph · February 22, 2017 · 5 Min read

Table of contents

Are you fretting of using that ancient, inexpressive and unsafe language to develop your Android Applications? Do you cry out loud whenever you see a Null Pointer Exception in your bug tracker? Now you do not need to think so much as Kotlin has come to your rescue. Kotlin is a new programming language specifically created and targeted towards Java developers.


What is Kotlin?


Kotlin is a high-performance new programming language developed by the guys at JetBrain, the company that is behind Intellij, Android Studio, and some of the best IDE in the industry. Essentially, Kotlin is an evolution of the Java syntax. Its source code can be compiled to JVM bytecode and it has 100% interoperability with Java. JetBrain even recently added the ability to compile Kotlin code to JavaScript, potentially broadening its reach.


The idea behind Kotlin


JetBrains wanted to provide a useful language that solves existing problems with Java. Kotlin has the following objectives mainly:


  • Provide an intuitive language that can infer from codes written by developers and do a lot of work on behalf of developers
  • Improve productivity by reducing the effort needed to write code
    Prevent null pointer exceptions
  • Provide Android app development support
  • Provide quick code compilation
  • Reduce learning time for developers by providing documentation and support
  • Overcome the issues Java developers face with Java


Who should use Kotlin?


Here question comes, who is Kotlin for? Newbies or experts?


With Kotlin, newbies will experience a cleaner and more concise syntax. It enables us to avoid a lot of beginner’s mistakes people usually run into with Java. Likewise, experts also enjoy the cleaner syntax and most likely find themselves more productive with it.


What makes Kotlin different?


Kotlin is a solution for those who hate Java. It solves issues related to Java, especially for Android. Though the next major release of Java (8) has taken place and it sounds pretty promising and brings a lot of modern language features such as lambda and functional programming, but it has not been made available for Android platform yet. Whereas Kotlin is available right now.


Let’s have a what it brings:


No more null pointer exceptions


Do you cry aloud every time when you run into a Null Pointer Exception while developing or testing? You don’t need to be fret anymore as Kotlin takes all null value checks from runtime to compile time. This process ensures null safety for any code that passes compilation.


See an example below, where Java code can be compiled, but will throw a Null Pointer Exception at runtime.


String a = null;


With Kotlin, variables are not nullable by default. For example, this piece of Kotlin code won’t compile:


val a:String = null
But if you really want to allow some variable to have null value, you can add a question mark:


val a: String? = null
In the example above, the first ? is used to make the variable a nullable, the second ? is just to check if the value of a is null or not.


Lesser coding


Unlike in Java, Kotlin compiler can understand from the code written by a developer and can develop or write the remaining code. Within Java, developer has to explicitly write everything that consumes a lot of time and efforts. Whereas Kotlin improves productivity while saving ample of time and effort.


For an example: Kotlin compiler can infer types in variable declarations as well as compiler-generated getters/setters/equals/hashCode. On the other side, Java compiler does not infer as much and requires the developer to explicitly write code.


Easy to learn and use for Java and Scala developers


Since JetBrains were intended to revolutionize Kotlin, any software developer can easily learn this new static language and make use of it especially the Java and Scala developers. Kotlin is dependent on Java in many ways and extensively uses the Java Library. Though it is inspired by Scala and according to many; it looks a lot like Scala. It uses the JVM code and the javac, the primary Java Compiler. The learning curve for Java developers is not steep and they can leverage the Kotlin documentation and get started quickly with coding.


Good for Android


Kotlin can do anything the Java language can do and more as well as offers backward compatibility for Java versions 6 and 7. In that way, Kotlin is available as a good tool for Android development. Since JetBrains has a plans for making Kotlin an Android-friendly language, they are working to speed up development workflow, reduce the size of the runtime library and provide more tooling support over the next few releases of Kotlin. The new language will be introducing a feature known as incremental compilation in its Gradle build plug-in. Kotlin can compile and build only the changes in the code and not the whole code over. Kotlin also plans to provide integration with the Android’s Jack and Jill toolchain.


There are lots and lots of things that Kotlin offers that allow a developer to be more productive, beyond what java 8 can offer to a java 6 developer. Since kotlin is easy to learn along with comprehensive documentation and interactive exercises, the use of Kotlin in Android development helps improve productivity significantly. So there is no good time as any to try kotlin in android app development.


Share this Article