Home Kotlin Basics
Post
Cancel

Kotlin Basics

The Kotlin programming language is a modern language that provides you with more power for your everyday tasks. Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. It can be used almost everywhere Java is used today for server-side development, Android apps, and much more. Kotlin class/file extension is kt. No need to put semicolon to the end of the code lines.

Intellij Idea can convert your code from Java to Kotlin. This is a vital feature for Java developers to learn Kotlin.


Variable Definitions

1
2
3
4
5
package hello
fun main() {
    var string: String  = "Hello, World!"  // defining a variable
    println("$string")
}

You can convert from Java code to Kotlin. Kotlin code can be converted to Java byte code.

  • Java source code -> .java
  • Kotlin source code -> .kt
  • Java byte code -> .class

.kt -> .class like .java -> .class

Class Definitions

  • Person class in Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    class Person {
      private String name;
      private int age;
    
      public Person(String name, int age) {
        this.name = name;
        this.age = age;
      }
    
      public String getName() {
        return name;
      }
    
      public int getAge() {
        return age;
      }
    }
    
  • Person class in Kotlin
    1
    2
    
    data class Person(val name: String, val age: Int)
    // data includes equals, hashCode, toString
    

Converting usages of the Person class

  • Java writing style to be used writing the name of the person to the console.
    1
    2
    
    Person person = new Person("Alice",27);
    System.out.println(person.getName());
    
  • Kotlin way
    1
    2
    
    val person = Person("Alice",27)
    println(person.name)
    

Switch-case Statements

  • updateWeather function in Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    public void updateWeather (int degress) {
      String description;
      Color color;
      if(degrees < 10) {
        description="cold";
        color = BLUE;
      } else if(degrees < 25) {
        description="cold";
        color = ORANGE;
      } else {
        description="hot";
        color = RED;
      }
    }
    

You can create an object without using new keyword in Kotlin.
new Abc(description, color)} => Abc("cold", BLUE)

  • updateWeather function in Kotlin
    1
    2
    3
    4
    5
    6
    7
    
    fun updateWeather(degrees: Int) {
      val (description, color) = when {
          degrees < 10 -> "cold" to BLUE
          degrees < 25 -> "mild" to ORANGE
          else -> "hot" to RED
      }
    }
    

    Passing parameters

    1
    2
    3
    
    fun main(args: Array<String>) {
      println("Hello, ${args.getOrNull(0)}!")
    }
    
    1
    2
    
    Hello, null!
    Hello, Kotlin!
    

    val / var usage

  • val -> read-only like a final variable immutable. read-only reference, not object
    1
    2
    
    val mutableList = mutableListOf("Java")
    mutableList.add("Kotlin")
    

    MutableList refers to an object in memory. We can’t assign another reference to the mutableList variable, but we can easily modify the exact object.

    Since you cannot modify a read-only list, the following lines of code is not going to be compiled.

    1
    2
    
    val readOnlyList = listOf("Java")
    readOnlyList.add("Kotlin") // doesn't compile
    

    By default, strive to declare all your new variables with ‘val’ keyword, not ‘var’. Prefer val to var. Using immutable references, immutable objects and functions without side effects makes your code closer to the functional style, which is easier to understand and support in the end.

  • var -> mutable. Local type inference. As you can see below, you can define variables in both ways. If you have assigned value, there is no need to specify the type of variable.
    1
    2
    
    val greeting : String = "Hi!"
    var number: Int = 0
    
    1
    2
    
    val greeting = "Hi!"
    var number = 0
    

    Compilation error because you can’t assign a string literal to a variable of Int type

    1
    2
    
    var string = 1
    string = "abc"
    

    I’ve tried to explain the basics of Kotlin language. In the next section, I will present examples of functions.

This post is licensed under CC BY 4.0 by the author.