10 Reasons Why Kotlin is Awesome and you should switch to Kotlin for Android Development

10 Reasons why Kotlin is awesome

If you have recently tried learning about Android Development or Java, odds are you have heard of Kotlin. Kotlin is a statically typed programming language that runs on the Java virtual machine. Kotlin is primarily developed by team of developers from JetBrains. It’s the same company which has developed famous IDE’s and tools such as IntelliJ Idea, PyCharm, RubyMine,

Kotlin has recently gained lot of popularity mostly because it has been adopted as official language for Android Development by Google.

Kotlin is 100% interoperable with Java and Android, which means you can use Kotlin in your existing Java projects. Java libraries and frameworks can be used alongside with Kotlin. Any libraries and frameworks built using Kotlin can be used with Java as well.

So here are 10 Reasons why Kotlin is awesome and why you should switch to Kotlin:

1) Java Interoperability

Kotlin is 100% interoperable with Java. You can use Kotlin in your Java projects and vice versa. You can easily use libraries and frameworks developed in Java in your Kotlin project and vice versa. This makes Kotlin easier to adopt.

Kotlin

// Calling Java code from Kotlin
class KotlinClass {
    fun kotlinDoSomething() {
        val javaClass = JavaClass()
        javaClass.javaDoSomething()
        println(JavaClass().prop)
    }
}

==================================

// Calling Kotlin code from Java
public class JavaClass {
    public String getProp() { return "Hello"; }
    public void javaDoSomething() {
        new KotlinClass().kotlinDoSomething();
    }
}

2) Null Safety

Kotlin does not allow null values by default. If you want to allow a variable or constant to have null value, you have to explicitly allow it with ! or ? operator. Writing safer code and avoiding null pointer exception is easier then ever.

Kotlin

var output: String
output = null   // Compilation error

==================================

val name: String? = null    // Nullable type
println(name.length())      // Compilation error

3) String Interpolation

Kotlin’s built in string interpolation is readable and easier to understand.

// Concatenation of Strings

val greeting = "Hello"
val name = "Sachin"
println("$greeting $name")

// Operations on numbers
val two = 2
val five = 5
println("Sum of $two and $five is ${two + five}")

4) Default and Named arguments

Kotlin allows us to provide default values for arguments. We can reduces the number of overloaded functions by using default arguments. Calling functions with named arguments also makes function much more readable.

// Kotlin

fun greet(name: String,
           greeting: String = "Hello") {
        println("$greeting $name")
    }

// Call function with default arguments.
greet("Thor") // Will Print `Hello Thor`

5) Type Inference

Kotlin will generally infer type from the context. You don’t have to state types explicitly.

// Kotlin

val name = "Thor" // type inferred to String
val count = 2 // type inferred to Int

val names: List<String> = ArrayList() // type declared explicitly 

6) Smart Casts

You don’t need to explicitly cast an immutable property or variable, if you check it using is operator for a specific instance type.

// Kotlin

fun getGreeting(obj: Any): String? {
    if (obj is String) {
        // `obj` is automatically cast to `String` in this branch
        return "Hello $obj"
    }

    // `obj` is still of type `Any` outside of the type-checked branch
    return "Hello Anonymous"
}

7) Data Class

With Kotlin you will have to write much less boiler plate code. Create POJO with getters, setters, equals(), hashCode(), toString(), and copy() with a single line.

// Kotlin

// Create a POJO with getters, setters, equals(), hashCode(), toString(), and copy() with a single line:
data class User(val name: String, val email: String)

8) Lambadas

Simplify your code by using Lambdas. This will help in reducing lot of boiler plate code.

// Java
button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        doSomething();
    }
});

// Kotlin
button.setOnClickListener { doSomething() }

9) Extend Functionality without inheritance

With Kotlin you can extend functionality of code without using inheritance. You can simply use extension function and properties to add functionality to existing classes.

// Kotlin

fun String.greet(): String {
        return "Hello $this"
}

val name = "Funny Guy"
name.greet() // Will print `Hello Funny Guy`

10) No need to use findViewById

findViewById is extensively used for finding views and viewgroups  in Android Development. It creates a lot of boilerplate code. You no longer need to call findViewById with Kotlin.

import kotlinx.android.synthetic.main.content_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // No need to call findViewById(R.id.textView) as TextView
        textView.text = "Kotlin for Android rocks!"

    }
}

The above mentioned advantages are just tip of the iceberg. Kotlin offers a lot more functionalities. Kotlin is fun to use. It improves readability, reduces boilerplace, helps in avoiding null pointer exceptions.

After rewriting code of our existing Java based Android apps  – All-in-one Unit Converter and Calculator and Currency Converter in Kotlin, we have observed reduction of more than 30% code and reduction of more than 70% null pointer exceptions.

Be the first to comment

Leave a Reply