Getting started with Kotlin

Apoorva Dave
Nerd For Tech
Published in
3 min readMar 20, 2021

--

Photo by Mikesh Kaos on Unsplash

This blog is mostly for beginners (like me ofcourse) who are planning to start learning Kotlin. It’s not a project or a tutorial but something that I feel I need on a daily basis. So without wasting any more minute, let’s dive in.

Declare a variable

Variables can be divided into two categories

  1. val: Read only (that cannot be modified)
  2. var: Variables declared as var can be reassigned and modified.

The best part here is variables can be assigned with any type be a String or an Integer.

So declaring a variable in kotlin is as simple as:

var thisIsModifiable = 1
val youCannotChangeMe = "abc"

You could always define the type of variable at the time of declaration. In the below example, we explicitly mention the type of the variable as Integer and to do that we use “:”

val thisIsInt: Int

Conditional statements

if else:

if (condition) { 
println("Inside if")
} else if (condition) {
println("Inside else if")
} else {
println("Inside else")
}

when: This is similar to switch statement in C.

when(x) {
1 -> println("x is 1")
2 -> println("x is 2")
else -> println("Neither 1 or 2")
}

Arrays, Lists, Set and Map

  • Arrays in Kotlin are represented by the Array class. They are invariant. This means that Kotlin does not let us assign an Array<String> to an Array<Any>, which prevents a possible runtime failure
val hellos = arrayOf("hi", "hello", "hola", "hello")
// [hi, hello, hola, hello]

Kotlin also has classes that represent arrays of primitive types without boxing overhead: ByteArray, ShortArray, IntArray . Example:

val x: IntArray = intArrayOf(1, 2, 3)
  • List is an ordered collection with access to elements by indices. Elements can occur more than once in a list.
val hellos = listOf("hi", "hello", "hola", "hello")
// [hi, hello, hola, hello]
  • Set is a collection of unique elements.
val hellos = setOf("hi", "hello", "hola", "hello")
// [hi, hello, hola]
  • Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value. The values can be duplicates.
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)println("All keys: ${numbersMap.keys}")  
println("All values: ${numbersMap.values}")
All keys: [key1, key2, key3, key4]
All values: [1, 2, 3, 1]

Kotlin provides two interfaces for each collection type — a read only and mutable. Note that altering a mutable collection doesn’t require it to be a var: write operations modify the same mutable collection object, so the reference doesn't change. Although, reassigning a val collection, will give a compilation error.

Loops

This could be one of the most important sections as this is where I am stuck most of the times 😛

For Loop: The for loop iterates through anything that provides an iterator.

val marks = listOf(1,2,3)
for(mark in marks) {
println(mark)
}

To iterate using indices of list, we can do something as below:

val marks = listOf(1,2,3)
for(i in marks.indices) {
println(marks[i])
}

There is also something known as forEach . Using forEach , we can simplify the looping logic.

val marks = listOf(1,2,3)
marks.forEach { mark ->
println(mark)
}

Defining a Function

fun myFirstFunction(x : Int, y: String) : Boolean {
var result = false
....
return result
}

Here we have defined a function named myFirstFunction with 2 arguments 1 integer and 1 string. This has a Boolean return type.

That is it! These are just few starting pointers for you all. This is definitely not sufficient but it will boost your confidence and help you learn Kotlin. Happy learning! :)

--

--