Kotlin – The Next Step for a Java Developer

7 min
Kotlin – The Next Step for a Java Developer

When I first meet Kotlin, I didn’t know what it especially is. I couldn’t understand quite special semantics and different structure. So, this is an article I’ve wanted to write for quite some time now. I have started working with Kotlin when tried to create an Android application and since that time I loved it.

Kotlin Programming Language

Kotlin in general, an open-source, statically-typed programming language that supports different programming paradigms like object-oriented or functional for example. Kotlin provides similar concepts from other languages and does not aim to be unique. It exists also in a different variants that target the JVM (Kotlin JVM {Java Virtual Machine}), JavaScript (Kotlin/JS) and Native code (Kotlin/Native).

Below you can find some Kotlin code what I previously wrote. I tried to take into account all important instructions and declarations to present basic usage of it. You can find there included functions and classes declaration and later usage, also basic loops, arrays and simple if statement as well. Before every section you can find a comment that explaining an action. So, have a nice lecture!

fun main() {
    // One line comment.
    /*
    	Multiline comment.
        We can add any information here.
    */
    
    // -----===== Standard operations on function and if statement =====-----
    var result: Double = sum_two_values(19.5, 20.2)
    var max: Double = 40.5
    if (result > max) {
        println("The result is greater than " + max.toString())
    } else {
        println("The result is lower than " + max.toString())
    }
    
    // -----===== WHEN (Case similar) statement =====-----
    var name: String? = readLine() // with the ? char, this varaible is Null Safety
    if (name == null) {
        name = "Wrong data...!"
    }
    when (name) {
        "Cezary" -> println("Oh, hello " + name)
        "Tomasz" -> println("The gate is open for you " + name)
        else -> {
        	println("Sorry, your name is not in the list. No entry possible.")
        }
    }
    
    // -----===== Array =====-----
    // Array -> Declaration example: var anyName = Array<DataType>(ArraySize[INT]){"DefaultValue"}
    // six length array containing strings with a default ""
    var names:Array<String> = Array<String>(2){""}
    names[0] = "Cezary"
    names[1] = "Tomasz"
    println(names[0])
    
    // -----===== ArrayList =====-----
    // More flexible data collection, we do not have declare the initial size
    // Declaration example: var anyName = ArrayList<String>()
    var cars = ArrayList<String>()
    cars.add("Volvo")
    cars.add("Fiat")
    cars.add("Honda")
    
    // -----===== Displaying the above cars array with for loop =====-----
    for (car in cars) {
        println(car)
    }
    
    // -----===== While loop =====-----
    var maxWhileLoop: Int = 10
    var currentIteration: Int = 0
   	while (currentIteration < maxWhileLoop) {
        println("Current iteration number is: " + currentIteration.toString())
        currentIteration ++
    }
    
    // -----===== Another example of For loop =====-----
    for (current in 1..10) {
        println("Current Forloop value: " + current.toString())
    }
    
    // -----===== Example of class uasage =====-----
    var firstContact = Contact(1, "Tomasz", "contact@sunnylib.com")
    firstContact.name = "Cezary"
    println(firstContact.get_full_contact_data())
    
}

// -----===== Example Function definition =====-----
fun sum_two_values(a:Double, b:Double):Double {
    var c:Double = a + b
    return c
}

// -----===== Example Class definition =====-----
class Contact(id:Int, name:String, email:String) {
    // basic variable declarations
    var id = 0
    	set(value) {
            if (value < 0) throw IllegalArgumentException("ID cannot be negative")
            field = value
        }
    var name = ""
    var email = ""
    
    // initialization 
    init {
        this.id = id
        this.name = name
        this.email = email
    }
    
    // Contact class simple method
    public fun get_full_contact_data():String {
        return "ID: " + this.id.toString() + "| " + this.name + ", " + this.email
    }
}

As you can see, Kotlin is very simmilar to Python. Of course, very similar is a big word for that but we can see way, which Kotlin’s creators are going to go or they was planning to go at the beginning. For me Kotlin is great and I think that I’ll stay with for a quite long time.


Post a Comment

Your email address will not be published. Required fields are marked *