...

Mobile App Development - Kotlin

Back to Course

Lesson Description


Lession - #398 Kotlin When


You can use the when expression instead of if..else, which is much easier to read.



Let's see an example



fun main(>
{ val day = 4 val result = when (day>
{ 1 -> "Monday" 2 -> "Tuesday" 3 -> "Wednesday" 4 -> "Thursday" 5 -> "Friday" 6 -> "Saturday" 7 -> "Sunday" else -> "Invalid day." } println(result>
}



Output



Try it here

Kotlin vs scala
Kotlin can be optionally typed while Scala always requires that you declare your variables before using them.

Kotlin vs golang
It provides less performance in comparison to Go.while It provides high performance in comparison to Kotlin.

KotlinInheritance is one of the more important features in object-oriented programming. Inheritance enables code re-usability, it allows all the features from an existing class(base class>
to be inherited by a new class(derived class>
. In addition, the derived class can also add some features of its own.The truth is data classes do not play too well with inheritance.


Syntax of inheritance:

open class baseClass (x:Int >
{ .......... } class derivedClass(x:Int>
: baseClass(x>
{ ........... }


The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming.

for beginner W3Schools Kotlin Compiler :