Why did Google choose Kotlin for Android? Part -1

Kotlin is a very popular programming language in nowadays. Thanks to JetBrains and Google.

Why Should I Switch From Java?

Java the official language of Android development and also one of the world’s most popular programming language (according to TIOBE ). So why should I switch?

Null-safety

One of the most common pitfalls in many programming languages, including Java, is that of accessing a member of a null reference, resulting in a null reference exception. In Java this would be the equivalent of a NullPointerException or NPE for short. Kotlin’s type system is aimed to eliminate NullPointerException’s from our code.

For more information

Less Code

Creating a Snackbar with Java

FloatingActionButton myfab = (FloatingActionButton) findViewById(R.id.myfab);
myfab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, “This is a snackbar”, Snackbar.LENGTH_LONG).setAction(“Action”, null).show();
}
});

With Kotlin

val myfab = findViewById(R.id.myfab) as FloatingActionButton
myfab.setOnClickListener { view ->
Snackbar.make(view, “This is a snackbar”, Snackbar.LENGTH_LONG)
.setAction(“Action”, null).show()

Let’s write a POJO class of our User

public class User {
private String firstName;
private String lastName;
 public String getFirstName() {
return firstName;
}
 public void setFirstName(String firstName) {
this.firstName = firstName;
}
 public String getLastName() {
return lastName;
}
 public void setLastName(String lastName) {
this.lastName = lastName;
}
}

but with kotlin:

class User {
var firstName: String? = null
 var lastName: String? = null
}
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.