Why Use Kotlin and why switch to this language?


Why Use Kotlin and why switch to this language?

This programming language developed by Jetbrains.(jet brains build Intellij IDE).if you are android developer then knows android studio is built on top of intellij IDE . The first official version 1.0 release was in Feb 2016. Now release version is 1.3. In google I/o 2017 kotlin is now official android language,kotlin language popularity grow and grow compared to other programming languages.

The question is this why we use kotlin?
-In kotlin we simply pass function within the function and apart from variable we can also pass function as a parameter

-Drastically reduce the amount of boilerplate code

boilerplate code

-Safety features:
Nullability: No more null pointer exception

-tool friendly( build from the command line in any Java IDE .)

-kotlin is more flexible java simply says kotlin is more powerful compare to java

-kotlin and java are interoperable:
They both can be used together in the same project and you can call into java language from kotlin and kotlin to java language.

1.Using variable :
var a:String ="Java"
val a=” “

val is immutable and var is mutable in Kotlin. simply says var value is change and val value can’t be changed.


for (item in collection) {
// body of loop

Example: how to apply condition inside for loop

for(i in 1..5)-->ranges
for (i in 5..1)
for (i in 5 downTo 1)
for (i in 5 downTo 2)
for (i in 1..5 step 2)
for (i in 5 downTo 1 step 2)

3. The When Expression: switch case is replaced in kotlin with when expression, see how it’s work
when (age) {
1 -> print("age is 1")
2 -> print("age is 2")
3, 4 -> print("age is 3 or 4")
in 5..10 -> print("age is 5, 6, 7, 8, 9, or 10")
else -> print("age is out of range")

4. Data Class: how easy is this, no need to make set and get method.

data class John(
val awards:Int,
val age:Int,
val email:String
val john = John(1, 20, "[email protected]")

5. inheritance and constructor: In kotlin how inheritance is work,
If use to inheritance then class should be open by open keyword:
open class car{

Now use constructor in this class

Remember: In primary constructor have init block when you write something in primary constructor you should use init and secondary constructor has own body .

Primary constructor

Primary constructor

Using inheritance and secondary constructor

fun main(args: Array){
var m=A(4,4,"black")

open class car {
var noOfDoors:Int=0
var noOfTyres:Int=0

constructor(noOfDoors: Int,noOfTyres: Int){

class A : car {
var color: String=""
constructor(noOfDoors: Int,noOfTyres: Int,color: String):super(noOfDoors,noOfTyres){

print("$color or $noOfDoors or $noOfTyres")

class B : car(4,4) {
lateinit var color: String
fun speed() {
println("highest speed is 200")


6. Prevents stack Overflow Exception:

Prevents stack Overflow

This solution in kotlin using (tailrec) method

Tailrec: In kotlin tailrec function that simply execute recursion internally without effected the stack memory.

. NullPointerExceptions, you know,it’s Billion Dollar Mistake.


. Kotlin protects you from nullable types

Kotlin protects

. If type is right then compiler will be auto cast it for you

then compiler will be auto

8.Extension functions:
. Add a new function to the class
. add function to a class without declaring it.the added function behave like a static.
. In simple words you will create an extension of a class without enter in class
See in the example you will better get it:

fun main(args: Array) {
val a = A()
println("Pass status: " + a.Passed(50))
println("fail status: " + a.fail(33))
fun A.fail(marks: Int): Boolean {
return marks < 33 } class A { // OWN CLASS fun Passed(marks: Int): Boolean { return marks > 33

Review Date
Reviewed Item
Why Use Kotlin and why switch to this language
Author Rating

    Next Big Technology