Kotlin for Android
Kotlin is combination of some of the best features of other languages. Also, Kotlin is quite similar to Apple’s Swift. Google noticed that some have officially received the programming language for their generation applications, including Pinterest, and Square. Kotlin will be amicable to those who have already used the Java programming language and also can be a ease to the beginers on Android.
You can include as meager or as much Kotlin into your current codebase as you need, blending the two languages effortlessly inside a similar venture.
There are now numerous excited Kotlin engineers for Android and the organization says it is just tuning into what the group needs.The CEO of JetBrains says “Kotlin is easy to get started with and can be gradually introduced into existing projects, which means that your existing skills and technology investments are preserved.” Hence, developers are going to enjoy Kotlin’s clean abstractions, concise syntax, and type safety. (Thank You, Google!)
Advantages of Kotlin
- Less amount of code, effort.
- Reduces occurrence of bugs due to concise coding.
- It can handle null pointer exceptions.
- It includes existing libraries for JVM, Android and Browser
- Its tool friendly
Why do we need Kotlin?
Kotlin can be preferred more due to following points :Stuck on Java
- No date time APIs Javax.time
- No Streaming APIs
- No language features Lamdas, method refs, non-capturing anon class
- No try with resources
Java language restrictions and problems
- Inability to add methods to platform types
- Nullability problems
- Mutability Problems
- General verbiosity of common idioms
Android API design problems
- Inheritance party
- Nullability everywhere
- Ceremony of APIs
How to convert Java to Kotlin?
- Update your Android Studio to 3.0.
- Select the java file from project structure that you need to convert to Kotlin.
- Android Studio Menu -> Code -> Convert Java File to Kotlin File. (From Android Studio Menu select Code, you will then find a dropdown which has Convert Java File to Kotlin File option at bottom, click that and its done).
Syntax
fun sum(a : Int, b : Int) : Int {return a+b
}
- sum - method name
- (a : Int, b : Int) - a,b variable name and Int its types
- : Int after the method - is return type
fun sum(a : Int, b : Int) = a+b
Kotlin can be used to build applications on JVM, Android and browser. Kotlin is useful as it includes few lines of code as say if a file written in java takes 20 lines of code can be wrapped up to 5-6 lines in Kotlin. As the code is less the proabability of the occurence of bugs is less on the same time the effort, time is less and the productivity and performance gets better.
Please do share your thoughts and any queries on this post in the comments below and please do like my post by giving +1 and by sharing it.
0 Comments