Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
In Kotlin, the switch
statement from languages like Java is replaced by the when
expression. It offers more flexibility and functionality than the traditional switch
.
Here's an example of using when
as a replacement for switch
:
fun main() { val x = 3 when (x) { 1 -> println("x is 1") 2 -> println("x is 2") 3 -> println("x is 3") else -> println("x is neither 1, 2, nor 3") } }
You can check for multiple conditions in a single branch:
when (x) { 1, 2 -> println("x is 1 or 2") 3 -> println("x is 3") else -> println("x is neither 1, 2, nor 3") }
when (x) { in 1..10 -> println("x is between 1 and 10") !in 10..20 -> println("x is not between 10 and 20") else -> println("x is something else") }
when
can also be used with expressions:
val str = "Hello" when (x) { str.length -> println("x is the length of the string 'str'") else -> println("x is something else") }
when
as an Expression:You can also use when
as an expression where it returns a value:
val description = when (x) { 1 -> "One" 2 -> "Two" 3 -> "Three" else -> "Unknown" } println(description) // This will print "Three" for x = 3
The when
expression can also be used as a replacement for lengthy if-else
chains:
when { x < 5 -> println("x is less than 5") x % 2 == 0 -> println("x is even") else -> println("x is something else") }
As demonstrated, the when
expression in Kotlin is a powerful construct that provides a concise way to handle multiple conditions.
Using when
expression as a switch in Kotlin:
when
expression as a replacement for traditional switch statements.when (condition) { 1 -> println("Case 1") 2 -> println("Case 2") else -> println("Default case") }
Switch vs when
in Kotlin:
when
expression.// Switch in Java switch (number) { case 1: System.out.println("Case 1"); break; case 2: System.out.println("Case 2"); break; default: System.out.println("Default case"); } // Equivalent in Kotlin using when when (number) { 1 -> println("Case 1") 2 -> println("Case 2") else -> println("Default case") }
Switch case with multiple conditions in Kotlin:
when
expression.when (condition) { in 1..5 -> println("Case 1 to 5") 10, 20 -> println("Case 10 or 20") else -> println("Default case") }
Switch with enum in Kotlin:
when
with enums for concise and readable code.enum class Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } val day = Day.MONDAY when (day) { Day.MONDAY -> println("It's Monday") Day.TUESDAY, Day.WEDNESDAY -> println("It's a workday") else -> println("It's the weekend") }
Handling default case in Kotlin switch:
when (condition) { 1 -> println("Case 1") 2 -> println("Case 2") else -> println("Default case") }
Switch statement for string values in Kotlin:
when
with string values for efficient string-based decision-making.val day = "Monday" when (day) { "Monday" -> println("It's Monday") "Tuesday", "Wednesday" -> println("It's a workday") else -> println("It's the weekend") }
Switch with ranges in Kotlin:
when
for concise and expressive comparisons.val score = 75 when (score) { in 0..50 -> println("Fail") in 51..75 -> println("Pass") in 76..100 -> println("Excellent") else -> println("Invalid score") }
Switch expression in Kotlin vs Java:
// Kotlin switch expression val result = when (condition) { 1 -> "Case 1" 2 -> "Case 2" else -> "Default case" } // Java switch statement String result; switch (condition) { case 1: result = "Case 1"; break; case 2: result = "Case 2"; break; default: result = "Default case"; }
Switching between multiple types in Kotlin:
when
can handle various types, making it more versatile.val value: Any = 42 when (value) { is String -> println("It's a String") is Int -> println("It's an Int") else -> println("Unknown type") }
Nested switch statements in Kotlin:
when
expressions for complex decision trees.when (condition) { in 1..5 -> { when (subCondition) { 1 -> println("Nested case 1") 2 -> println("Nested case 2") } } 10, 20 -> println("Case 10 or 20") else -> println("Default case") }
Switching on custom objects in Kotlin:
data class Color(val name: String) val color = Color("Red") when (color) { Color("Red") -> println("It's a red color") Color("Blue") -> println("It's a blue color") else -> println("Unknown color") }
Switch statement alternatives in Kotlin:
when
.// Using if-else as an alternative to switch val result = if (condition == 1) "Case 1" else if (condition == 2) "Case 2" else "Default case" // Using a Map for mapping values to actions val actionMap = mapOf(1 to "Action 1", 2 to "Action 2") val result = actionMap[condition] ?: "Default action"
Pattern matching with when
in Kotlin:
when
in Kotlin for more advanced matching scenarios.val result = when (val response = fetchData()) { is Success -> "Data fetched successfully" is Error -> "Error fetching data: ${response.message}" }