Blog Details

Why you should totally switch to Kotlin from JAVA

I want to tell you about a new programming language called Kotlin and why you should consider it for your next project. I used to prefer Java but the last year I’ve found myself coding Kotlin whenever I could, and at this point I really can’t think of a situation where Java would be a better choice.

It’s developed by JetBrains, and the fact that these are the people behind a suite of IDEs, such as IntelliJ and ReSharper, really shines through in Kotlin. 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 (in no particular order):

0# JAVA Interoperability:

Kotlin is 100% interoperable with Java. You can literally continue work on your old Java projects using Kotlin. All your favorite Java frameworks are still available, and whatever framework you’ll write in Kotlin is easily adopted by your stubborn Java loving friend.

1# Familiar Syntax

Kotlin isn’t some weird language born in academia. Its syntax is familiar to any programmer coming from the OOP domain, and can be more or less understood from the get go. There are of course some differences from Java such as the reworked constructors or the val var variable declarations. The snippet below presents most of the basics:

class Foo {
    val b: String = "b"     // val means unmodifiable
    var i: Int = 0          // var means modifiable
    fun hello() {
        val str = "Hello"
        print("$str World")
    fun sum(x: Int, y: Int): Int {
        return x + y
    fun maxOf(a: Float, b: Float) = if (a > b) a else b

 2# String Interpolation

It’s as if a smarter and more readable version of Java’s String.format() was built into the language:

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

3# Type Inference

Kotlin will infer your types wherever you feel it will improve readability:

val a = "abc"                         // type inferred to String
val b = 4                             // type inferred to Int
val c: Double = 0.7                   // type declared explicitly
val d: List<String> = ArrayList()     // type declared explicitly

4# Smart Casts

The Kotlin compiler tracks your logic and auto-casts types if possible, which means no more instanceof checks followed by explicit casts:

if (obj is String) {
    print(obj.toUpperCase())     // obj is now known to be a String

5# Intuitive Equals

You can stop calling equals() explicitly, because the == operator now checks for structural equality:

val john1 = Person("John")
val john2 = Person("John")john1 == john2    // true  (structural equality)
john1 === john2   // false (referential equality)

6# Default Arguments

No need to define several similar methods with varying arguments:

fun build(title: String, width: Int = 800, height: Int = 600) {
    Frame(title, width, height)

7# Named Arguments

Combined with default arguments, named arguments eliminates the need for builders:

build("PacMan", 400, 300)                           // equivalent
build(title = "PacMan", width = 400, height = 300)  // equivalent
build(width = 400, height = 300, title = "PacMan")  // equivalent

8# The When Expression

The switch case is replaced with the much more readable and flexible whenexpression:

when (x) {
    1 -> print("x is 1")
    2 -> print("x is 2")
    3, 4 -> print("x is 3 or 4")
    in 5..10 -> print("x is 5, 6, 7, 8, 9, or 10")
    else -> print("x is out of range")

It works both as an expression or a statement, and with or without an argument:

val res: Boolean = when {
    obj == null -> false
    obj is String -> true
    else -> throw IllegalStateException()

9# Properties

Custom set & get behavior can be added to public fields, which means we can stop bloating our code with mindless getters & setters.

class Frame {
    var width: Int = 800
    var height: Int = 600

    val pixels: Int
        get() = width * height

10# The Data Class

It’s a POJO complete with toString()equals()hashCode(), and copy(), and unlike in Java it won’t take up 100 lines of code:

data class Person(val name: String,
                  var email: String,
                  var age: Int)

val john = Person("Prateek", "", 112)

And that’s all for now. Thank you for reading! This is my first ever medium post. If you are not yet convinced about Kotlin here is some more convincing:

Original Blog from Medium: Medium


Leave a Comment