Why should you totally switch to Kotlin ?
By: Eesha Srivastava
Tags: Kotlin, Code, Kotlin Beginners

JetBrains developed IntelliJ IDEA, which is the basis for Android Studio. In 2011, the company introduced the Kotlin language, but few Android developers started using it. Though Kotlin was production ready, the language wasn’t stable. When important changes in the language happened, developers had to change their codebase.

In 2017, Google announced that Android will support Kotlin as a first-class programming language so for this to happen, the 3.0 release of Android Studio integrated Kotlin support out of the box!

Kotlin is a statically-typed, modern programming language that runs on a Java Virtual Machine (JVM) by compiling Kotlin code into Java byte-code. It’s pragmatic and concise and makes coding a satisfying and efficient experience.

So here’s a couple of reasons why you should totally switch to Kotlin:

Familiar Syntax: One of the advantages of Kotlin is that we get more functionality with less lines of code. Kotlin is a concise language, with estimates of being less verbose than Java.

class Example { val b: String = "b" // val means ‘value cannot be changed’ var i: Int = 0 // var means ‘value can be changed’ fun message() { val str = "Hello" print("$str World") }

2. Avoid NullPointerException: Facing issues with ‘null’ ? Kotlin introduces a new and secure way to deal with nulls. If the variable is nullable, the compiler will not permit you to access it without a proper check. Kotlin forces you to use a safe call operator, ?. operator.

Question mark ‘?’ at the end of the variable is used to differentiate null variables from other normal variables in Kotlin.

val a = "Kotlin" val b: String? = null println(b?.length) println(a?.length)

This returns b.length if b is not null, and null otherwise.

So with this solution at least your application will not crash implicitly via NullPointerException anymore.

3. Type Inference: Type means ‘Data Type’; Inference means ‘To automatically identify something.’ Kotlin compiler can automatically identify the data type of the variable.

For example- if we are assigning any whole number to a variable, the compiler will identify by itself that the variable is of type Int.

You can write val a = 10 instead of val a : Int = 10

4. When expression: ‘when’ expression in Kotlin is a conditional expression which returns the value. It is used in replacement of switch statements of languages such as Java, C++, C.

fun main(args: Array<String>){ var a = 4 var numberProvided = when(a) { 1 -> "One" 2 -> "Two" 3 -> "Three" 4 -> "Four" 5 -> "Five" else -> "invalid number" } println("You provide $numberProvided")

5. No setters and getters: In Kotlin, getters and setters are auto-generated in the code if you do not create them in your program. Getters are used for getting value of the property. Similarly, setters are used for setting value of the property.

The following code in Kotlin-

class Person { var name: String = "defaultvalue" get() = field // getter set(value) { field = value } // setter } val obj = Person() obj.name = "Hello World!" // access setter println(obj.name) // access getter

(Output: Hello World!)

When we are trying to access the name property of the object, we get field because of this code, get() = field.

We can get or set the properties of an object of the class using the dot(.) notation.

6. The Data Class: There may arise a situation where you need to create a class solely to hold data. Data class is a simple class which is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class.

data class Student(val name: String, val age: Int) fun main(args: Array<String>) { val stu = Student("Mitali", 31) val stu2 = Student("Ajeet", 30) println("Student Name is: ${stu.name}") println("Student Age is: ${stu.age}") println("Student Name is: ${stu2.name}") println("Student Age is: ${stu2.age}") }

7. Extension Functions: Extension functions are great as they completely remove the need for the various “Util” classes in Java. They allow us to declare functions on already existing types.

For example, if we want to remove first and last character of the string-

fun String.removeFirstLastChar(): String = this.substring(1, this.length - 1) fun main(args: Array<String>) { val myString= "Hello Everyone" val result = myString.removeFirstLastChar() println("Result is: $result") }

When you run the program, the output will be:

Result is: ello Everyon

Here, an extension function removeFirstLastChar() is added to the String class.

8. String Interpolation: Kotlin’s string interpolation is an amazing feature. A smarter version of Java’s String.format() is implemented in this feature.

val x = 4 val y = 7 print("sum of $x and $y is ${x + y}") // sum of 4 and 7 is 11

9. Default Arguments: Short and easy to use, one can provide default values to parameters in function definitions. So, this removes the need to to define several similar methods with varying arguments.

10. Java Interoperability: Even if you’re having your old projects written in Java, you can still continue to work using Kotlin. Kotlin is 100% interoperable with Java.