What's the benefit of switching existing Java code in Android app to Kotlin?
I am reading a lot about Kotlin lately in blogs. But I am not sure what seems to be the benefit of switching the existing app into Kotlin. Is there any specific feature or performance gain one can get with switch to Kotlin?
The benefit is mostly during development, there is not much performance difference. If the app is still being developed or actively maintained, I think it's worth switching!
Kotlin is philosophically a fairly similar language to Java, focused on object orientation and inheritance, with static typing, so it's not hard to learn. You can also do a large part of converting automatically.
But despite the similarity, is fixed a lot of problems that Java has but that Java cannot fix due to a strong focus on backward compatibility. These help you write more robust code and prevent a lot of boilerplate.
Some 'defects' are fixed, e.g. no more primitives, issues relating to
It saves a lot of code, e.g. there is type inference, constructors are super short, types are cast automatically if you do e.g. x is Int, there are string templates ("hello, $x"), data classes (toString, equals, hashCode), automatically calling getters/setters on property access.
Values by default cannot be null. You have to specify if you want them to be nullable, in which case Kotlin will force you to check before using them. No more null pointers! Collections are also immutable by default, making them much safer to pass around (but they can be mutable when needed).
Encourages writing better code, e.g. all fields private, no static fields/methods, much more powerful sum types (sealed classes instead of enums), operator overloading (subjective but I think it's much better).
I think any new software, and quite some old software, should be made in Kotlin instead of Java (if those are the options). While not everything about Kotlin is perfect, I personally feel that a lot is better than Java, and nothing is worse (on a technical level - popularity is still well behind Java outside Android).
Mark
formerly known as M
The benefit is mostly during development, there is not much performance difference. If the app is still being developed or actively maintained, I think it's worth switching!
Kotlin is philosophically a fairly similar language to Java, focused on object orientation and inheritance, with static typing, so it's not hard to learn. You can also do a large part of converting automatically.
But despite the similarity, is fixed a lot of problems that Java has but that Java cannot fix due to a strong focus on backward compatibility. These help you write more robust code and prevent a lot of boilerplate.
Some highlights (you can read more here):
x is Int, there are string templates ("hello, $x"), data classes (toString, equals, hashCode), automatically calling getters/setters on property access.null. You have to specify if you want them to be nullable, in which case Kotlin will force you to check before using them. No more null pointers! Collections are also immutable by default, making them much safer to pass around (but they can be mutable when needed).sealedclasses instead of enums), operator overloading (subjective but I think it's much better).I think any new software, and quite some old software, should be made in Kotlin instead of Java (if those are the options). While not everything about Kotlin is perfect, I personally feel that a lot is better than Java, and nothing is worse (on a technical level - popularity is still well behind Java outside Android).