Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
In Kotlin, as in many programming languages, you'll encounter the terms "expression," "statement," and "block." Understanding the difference between these is crucial as you learn to read and write Kotlin code. Let's dive into what each of these terms means:
An expression in Kotlin is a combination of variables, values, and operators that is evaluated to produce a value. Functions can also be part of expressions, provided they return a value.
Examples:
val sum = 3 + 5 // 3 + 5 is an expression that evaluates to 8 val result = if (sum > 10) "Greater" else "Smaller" // The entire if-else construct here is an expression.
In the second example, the if-else
construct in Kotlin acts as an expression because it returns a value.
A statement in Kotlin is a unit of code that represents an action or command to be executed. In contrast to expressions, statements do not produce values. In fact, in Kotlin, most things that are statements in other languages (like assignments or loops) are expressions. However, not every expression can be considered a statement.
Examples:
val number = 5 // This is a declaration statement. println("Hello, World!") // This is a function invocation statement.
In Kotlin, fewer constructs are purely statements compared to languages like Java because many Kotlin constructs can return values (and are thus expressions).
A block in Kotlin is a group of statements (and possibly expressions) enclosed in curly braces { }
. A block is itself an expression if it returns a value, which is the value of the last expression in the block.
Example:
val maxNumber = if (a > b) { println("Choosing 'a'") a // Last expression's value ('a') is the value of the whole block. } else { println("Choosing 'b'") b // Last expression's value ('b') is the value of the whole block. }
In this example, the blocks inside the if
and else
branches are expressions because they return a value.
Understanding these fundamental concepts can help you read and write Kotlin code more effectively and also helps in grasping more complex Kotlin features built on these basics.
Examples of Kotlin expressions:
val sum = 3 + 5
Kotlin single-line vs multi-line expressions:
val sum = 3 + 5
val result = run { val a = 10 val b = 20 a + b }
Kotlin conditional expressions:
val result = if (condition) "True" else "False"
Kotlin return statements:
return
is used to exit a function and provide a result.fun add(a: Int, b: Int): Int { return a + b }
Blocks and scoping in Kotlin:
fun example() { val a = 10 if (condition) { val b = 20 println(a + b) } // b is not accessible here }
How to use expressions in Kotlin functions:
fun multiply(a: Int, b: Int): Int = a * b
Kotlin when expression usage:
when
expression is a powerful switch-like construct.val result = when (value) { 1 -> "One" 2 -> "Two" else -> "Other" }
Kotlin if-else expressions:
if-else
can be used as an expression.val result = if (condition) "True" else "False"
Statements in Kotlin loops:
for (i in 1..5) { println(i) }
Kotlin lambda expressions as statements:
val printMessage: () -> Unit = { println("Hello") }
Use of semicolons in Kotlin:
val a = 5; val b = 10