Kotlin

Unit in Kotlin corresponds to Void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

Variables

Var: Mutable Variable

Val: Immutable Variable, like final in Java.

Types are non-null by default in Kotlin. If nullable value is needed, then we can add “?” (Question marks) after the variable types.

val name: String? = null

Kotlin will also recognizes the String value automatically in most cases.

val name = "Kotlin"
var gender = "Non Binary"

Expressions

In Kotlin, an expression may be used as a statement or used as an expression depending on the context. As all expressions are valid statements, standalone expressions may be used as single statements or inside code blocks.

If Conditional Expressions

If statements in Kotlin are simple and can be used as a Boolean.

fun main() {
	val a = 3
	val b = 2

	if (a > b) println("a is bigger than b")
	else println("a is not bigger than b")
}

When Expressions

When expressions are very similar to Switch statements in Java. The only difference is the When Statements support “Else”(As in If Statements). So that makes the When Statements more flexible to code with.

Example of When expressions when used to replicate an If Conditional:

fun main() {
	var age:Int = 10
	when(age){
		10 -> println("Age is 10")
		else -> println("Age is not 10")
	}
}

Functions

Calling functions:

fun getGreeting(): String {
    return "Hello Kotlin"
}

fun main() {
    println("Hello world")
    println(getGreeting())
}

Function Parameter:

fun sayHello(itemToGreet:String){
    val msg = "Hello " + itemToGreet
    println(msg)
}

fun main(){
    sayHello("Kotlin")
}

Double Parameter:

fun sayHello(greeting:String, itemToGreet:String) = println("$greeting $itemToGreet")


fun main(){
    sayHello("Hello", "Kotlin")
}

The “Hello” is using the first parameter $greeting, and the “Kotlin” is using the second parameter $itemToGreet.

First parameter = $greeting = “Hello”

Second parameter = $itemToGreet = Kotlin

Higher Order Functions

Kotlin - Apply, Let, Run, With, Also - Higher Order Standard Functions
Kotlin is all about simplifying the development. Kotlin's standard library is full of useful classes and functions which aim to fulfill this promise even where the built-in language features don't exist. One of such groups of functions in standard library are Kotlin's higher order functions apply, let, run, with and also.
https://www.youtube.com/watch?v=-Lha9n8VJf0

Collections & Iterations

In Kotlin, there’s various types of collections & iterations. Some of which are Arrays, Lists, and Maps.

The basic difference between Array and Lists are: Array is static, while lists are dynamic.

In Collections, Kotlin also differentiate between mutable and immutable collection types.

By defaults, a collection type in Kotlin is immutable.

By immutable it means that you cant add or subtract values from that collections once its initially created.

Arrays

fun main(){
    val phone = arrayOf("iPhone", "Pixel", "Samsung")
    println(phone.size)
    println(phone.get(0))
}

Lists

fun main(){
    val phone = listOf("iPhone", "Pixel", "Samsung")
    println(phone.size)
    println(phone.get(0))
}

Show all Arrays

fun main(){
    val phone = arrayOf("iPhone", "Pixel", "Samsung")
    
    for (phone in phone){
        println(phone)
    }
}

Foreach

Foreach takes in another function and returns Unit. This Foreach method is applicable to Lists as well.

fun main(){
    val phone = arrayOf("iPhone", "Pixel", "Samsung")
    
    phone.forEach{
        phone -> println(phone)
    }
    
}

It. It is the default name for each element in the array that is passed into this Lambda function in which we are defining.

fun main(){
    val phone = arrayOf("iPhone", "Pixel", "Samsung")
    
    phone.forEach{
        println(it)
    }
    
}

ForEachIndex. ForEachIndex, like its name; is used to find index within Arrays. Use this code to see each Value and which Key are they assigned with, we can use this code.

fun main(){
    val phone = arrayOf("iPhone", "Pixel", "Samsung")
    
    phone.forEachIndexed{
        index, phone -> println("$phone is at $index")
    }
    
}

Map

fun main(){
    val phone = mapOf(1 to "iPhone", 2 to "Pixel", 3 to "Samsung")
    println(phone.size)
    println(phone.get(1))
}

With map you can map and assigns Key to the Value. Like above example, the first Value (“iPhone”) is assigned to Key 1. In default, the Arrays and Lists assigns the first Value to Key 0. But with map, you can reassign every Value into any Keys.

As you can see below, when we requested the Value of Key 0, the output will be Null.

fun main(){
    val phone = mapOf(1 to "iPhone", 2 to "Pixel", 3 to "Samsung")
    println(phone.size)
    println(phone.get(0))
}

Map ForEach

Basically ForEachIndex.

fun main(){
    val phone = mapOf(1 to "iPhone", 2 to "Pixel", 3 to "Samsung")
    phone.forEach{key, value -> println("$key -> $value")}
}

Mutable

Since all collections in Kotlin are immutable by default, to add a mutable collections we have to add functions such as mutableMap.

fun main(){
    val phone = mutableMapOf(1 to "iPhone", 2 to "Pixel", 3 to "Samsung")
    phone.put(4, "Blackberry")
    phone.forEach{key, value -> println("$key -> $value")}
}

Reference

Learn the Kotlin programming language in this introduction to Kotlin. Kotlin is a general purpose, open source, statically typed "pragmatic" programming language. It is used for many things, including Android development. 🎥Course developed by Nate Ebel.
https://www.youtube.com/watch?v=F9UC9DY-vIU
Video stopped at 55:00