Thu Dec 28 2017
Advantages of Kotlin over Java in case of Android development
When you think about Android development, only one programming language immediately springs in your mind: Java. But, Google changed this tradition by announcing Kotlin as an official language for Android development at I/O 2017. Kotlin is the most recent, lightweight and powerful language that overcome several problems suffered by Android developers using Java such as verbosity of source code and run-time exceptions and many others. Let's find it's advantages over Java in the case of Android development.
Interchangeability With Java
Kotlin's greatest strengths as a potential alternative to Java. There have the sheer level of interoperability between Java and Kotlin. You can even have Java and Kotlin code existing side by side in the same project, and everything will still compile perfectly. In case of interoperability, if you do have a project that you're already working on, you can trial Kotlin in a small part of that project without affecting the rest of your code base.
Type aliases
It is a simple, yet very useful feature. Type aliases will save some keystrokes and make the code more readable. Basically, It enables us to assign an alternative name, or alias, to any given type. This is most useful for long types with many generic parameters.
Concise Code
Both Java and Kotlin class produce the same results, but the code in Kotlin is much more succinct and compact than the code in Java. Kotlin is good at reducing the amount of boilerplate code, which should make coding in Kotlin a much more enjoyable compare to coding in more verbose languages such as Java.
Data classes
Most of our applications are data driven, we often find ourselves creating classes with only properties and fields to hold data. With Kotlin, we can declare the class and all its properties in a single line and the compiler will generate all getters and setters, as well as the equality members. It saves lots of time of a developer.
Extension functions
Extension functions are a great feature, which help in building really clean APIs and solve a bunch of other problems. With Kotlin, developer can add behavior to a class without directly extending it or using a design pattern like Decorator.
Higher-order and lambda functions
Higher order functions are one of the main feature of Kotlin. It takes functions as parameters, or returns a function. One major use case for this is callback functions. We can create a function that can call with two callbacks - one for success and another for error. Functions can be stored in variables, passed around, or created inside another function. Kotlin also support a lambda or an anonymous function, which is not declared, but passed immediately as an expression.
Null safety
Kotlin eliminates most sources of null references by making all types non-nullable. That means the compiler won't let you use a non-initialized, non-nullable variable. If you need a variable to hold a null value, you have to declare the type as nullable by adding a question mark after the type.
Coroutines
When a developer need to perform a long-running task, such as a network operation or loading a file from disk, the calling thread gets blocked waiting for the operation to complete. In this case, coroutines enable us to execute these types of operation without blocking a thread. It is a lighter operation called suspension of a coroutine.