switch
statement like in some other programming languages such as Java or C++. Instead, Kotlin offers the when
expression, which serves as a more powerful and flexible alternative. The when
expression can be used for pattern - matching and can replace the if - else if
chains in a more concise and readable way. This blog post will delve into the core concepts, typical usage scenarios, and best practices of the Kotlin when
expression, providing intermediate - to - advanced software engineers with a solid understanding of how to use it effectively.when
when
The when
expression in Kotlin is a control flow construct that evaluates a value and matches it against a series of conditions. It can be used either as a statement (when it doesn’t return a value) or as an expression (when it returns a value).
Here is the basic syntax of a when
expression:
when (expression) {
value1 -> {
// code to execute when expression == value1
}
value2 -> {
// code to execute when expression == value2
}
else -> {
// code to execute when no other condition is met
}
}
This is the most basic use case of the when
expression, where you match a value against a set of constants.
fun getDayOfWeekName(day: Int): String {
return when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day"
}
}
fun main() {
val dayNumber = 3
println(getDayOfWeekName(dayNumber)) // Output: Wednesday
}
In this example, the when
expression takes an integer day
and returns the corresponding day of the week name. If the day
value is not between 1 and 7, it returns “Invalid day”.
You can use ranges in the when
expression to match a value within a certain range.
fun getGrade(score: Int): String {
return when (score) {
in 90..100 -> "A"
in 80..89 -> "B"
in 70..79 -> "C"
in 60..69 -> "D"
in 0..59 -> "F"
else -> "Invalid score"
}
}
fun main() {
val score = 85
println(getGrade(score)) // Output: B
}
Here, the when
expression checks which range the score
falls into and returns the corresponding grade.
The when
expression can also be used for type checking and casting.
fun printValue(value: Any) {
when (value) {
is String -> println("The string is: $value")
is Int -> println("The integer is: $value")
is Double -> println("The double is: $value")
else -> println("Unknown type")
}
}
fun main() {
val number: Any = 42
printValue(number) // Output: The integer is: 42
}
In this example, the when
expression checks the type of the value
parameter and performs different actions based on the type.
You can specify multiple conditions in a single when
branch.
fun isWeekend(day: Int): Boolean {
return when (day) {
6, 7 -> true
in 1..5 -> false
else -> false
}
}
fun main() {
val day = 6
println(isWeekend(day)) // Output: true
}
Here, the when
expression checks if the day
is either 6 or 7 (Saturday or Sunday), and returns true
if it is a weekend day.
else
Branch: Always include an else
branch in your when
expression to handle cases that don’t match any of the specified conditions. This helps prevent unexpected behavior.when
expression too long or complex. If it becomes too large, consider refactoring the code into smaller functions.when
for type checking and casting. The compiler will ensure that the types are correct.The Kotlin when
expression is a powerful and flexible alternative to the traditional switch
statement. It offers a wide range of capabilities, including simple value matching, range matching, type checking, and handling multiple conditions. By following the best practices, you can write more concise, readable, and maintainable code. Understanding and effectively using the when
expression will enhance your Kotlin programming skills and make your code more robust.