Nowadays Kotlin is a general purpose language, used by many developers to build different types of applications, especially in Android Application Development. But you can use it anywhere whether it is Server Side Application, Browser Application or Mobile Application. I am going to tell you here the most amazing things about Kotlin you must know, why the future is Kotlin for many software developers.
-
Null Safe – Most of the bugs in software development because of the NullPointerException. Kotlin supports null safe checks to avoid this exception. For example,
var name: String? = null
name?.substring(0, name.length)
It will not throw any exception here. It will call the substring function only if the name is not null. But in Java it will throw NullPointerException.
-
Data Classes – We can create classes to hold data without any getter and setter methods by using a data keyword. Kotlin will take care of it.
For example,
data class Student(val name: String, val age: Int)
The same class in java is used to be like
public class Student {
private String name;
private int age;
Student() {}
public String getName() {
return name;
}
public setName(String name){
this.name = name;
}
public int getAge() {
return age;
}
public setAge(int age){
this.age = age;
}
}
-
Efficient– Kotlin is way more concise than Java. Kotlin is easy to learn and the syntax is intuitive. You don’t need to write a lot of code as compared to Java. For example,
val list = mutableListOf("one", "two", "one", "four")
If you want to filter above list with string “one”
val filteredList = list.filter { item ->
Item == “one”
}
-
Functional programming – Like in Python, functions in Kotlin can be assigned to variables and passed around as parameters. For example,
fun print(function: (Int, Int) -> Double) {
println(function(2, 0))
}
print({ numerator, denominator ->
if (denominator == 0) 0.0 else numerator.toDouble() / denominator
})
-
Interoperability – Kotlin can be easily used with Java. You can run Java codes on Kotlin and vice versa. It makes developer’s lives easier and more productive.
-
Smart Cast – From a developer’s point of view, it is very useful to reduce the speed of application and improve performance.You can perform all operations if you have identified the type of a variable. For example,
val name : Any = “Sachin Saxena”
when (name) {
is String -> name.length
is Int -> n.inc()
}
-
Type inference – In Kotlin, we don’t need to specify the type of each variable explicitly. If we want to specify explicitly we can do so. For example,
Java - String name = “Sachin Saxena”
Kotin - val name = “Sachin Saxena” (automatically variable ‘name’ is of type String)
Summing Up
Kotlin is the future of Android app development. It is widely used by big companies like Amazon, Google etc for Android Development as well as Server Side Application. Use of Kotlin has increased significantly for Android development as we can see in the above graph.
If you want to learn Kotlin in more details, go to Kotlin Home Page.
Very helpfull tutorial…