Kotlin Fundamental

Why use Kotlin for Android Development?

Pinterest LinkedIn Tumblr

Welcome Guys, In this post I’ll tell you why you should use Kotlin for Android development? Android discusses all advantages of Kotlin over Java in Android for Android Development. Why Kotlin is more powerful than Java

Kotlin

It is a Multi-Platform, concise, expressive, and powerful programming language. Best thing, it’s interoperable with Java Virtual Machine and Android Runtime.

Kotlin is modern safe and concise while being expressive.  Kotlin is adapt and be adaptable to different like –  you can build server application front- end application as well as Android application.

It contains safety feature for nullability,  Avoid an entire class of null pointer exception. Handle immutable of variable threw out the applications. Kotlin help to make a healthy app and improve of the application.

Kotlin is easy to learn for an existing developer, especially for Java developer. The interoperable feature makes more easy to use in an existing Java project.

1. Safer Code

Kotlin contains nullability feature. The Kotlin has eliminated the danger of null references from code throughout the app,  write safe code and avoid NullPointerExceptions exception.

Normally, In Java you declare like this
String userName;
userName = null;
But Kotlin handle this state using !! and ? operator

(!! operator indicates not nullable,? the operator tells the compiler it can be null)

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

2. Concise

Focus on expressing your ideas and write less boilerplate code you need to write. Create a POJO with equals(), getters, setters, hashCode(), and copy(), toString() with a single line like below.

data class LoginUser(val userName: String, val email: String)

3. Lambda Expression

Kotlin natively lambdas expression, that simplifies your code and reduces the number of line of the code in Android. You thinking hows .?? check below example

In Java you write click listener code like below.
button.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        loginUser();
    }
});
In Kotlin you need write single line  code
button.setOnClickListener { loginUser() }
Reduce the number of overloaded functions by using default arguments. Call functions using named arguments to make your code more readable.
fun format(str: String,
           normalizeCase: Boolean = true,
           upperCaseFirstLetter: Boolean = true,
           divideByCamelHumps: Boolean = false,
           wordSeparator: Char = ' ') {
      //....
    }
// You can call Call function with named argument eg.
format(str, normalizeCase = true, upperCaseFirstLetter = true)

4. Forgot about findViewById

In Kotlin, You now need to call findViewById in Kotlin you can access all views component directly using ID.

In Java, you bind view like this way
TextView textHello = (TextView) findViewById(R.id.textView);
textHello.setText("The AndroidWave | Leading App Developer in India !")
How to access component in 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 = "The AndroidWave | Leading App Developer in India"
}

5. 101% Interoperable with Java

Interoperable is played an important role in your while you learn a new language. Interoperability means you can use all java code in in a single click.  Everything you can do with Java, you can do in Kotlin. Kotlin is a JVM language that’s completely interoperable with Java

Way of calling Java Code in Kotlin
class KotlinClass {
    fun kotlinDoSomething() {
        val javaClass = JavaClass()
        javaClass.javaDoSomething()
        println(JavaClass().prop)
    }
}
Way of Calling Kotlin code from Java
public class JavaClass {
    public String getProp() { return "Hello"; }
    public void javaDoSomething() {
        new KotlinClass().kotlinDoSomething();
    }
}

6. Smart Casting

Below I’m describing uses a feature of Kotlin called Smart Cast. , Let’s compare how we do class casting in Java vs Kotlin.

In Java, We first check the type of the variable using the instanceof operator and after that will cast it to the target type like this 

Object obj = "TThe AndroidWave | Leading App Developer in India";
if(obj instanceof String) {
    // Explicit Casting to `String`
    String str = (String) obj;
    System.out.println("Found a String of length " + str.length());
}

But In Kotlin, When you perform an i is or !is check on a variable, the compiler tracks this information and automatically casts the variable to the target type in the scope where is!is is true.

val obj: Any = "The AndroidWave | Leading App Developer in India"
if(obj is String) {
    // The variable obj is automatically cast to a String in this scope.
    // No Explicit Casting needed. 
    println("Found a String of length ${obj.length}")
}

7. Default Arguments

class Developer(val name: String,
 val age: Int,
 val someValue: Int = 0,
 val profile: String = “”) {
}
 
val amit = Developer(“Amit Shekhar”, 22, 10, “Android Developer”)
val anand = Developer(“Anand Gaurav”, 20, 11)
val ravi = Developer(“Ravi Kumar”, 26)

8. Great Tooling Support

Android Studio 3.0 provides helpful tools to help you start using Kotlin. Convert entire Java files or convert code snippets on the fly when you paste Java code into a Kotlin file.

1 Comment

  1. Adekanmbi Yosola Reply

    Would you be writing your later tutorials in kotlin too?

Write A Comment