Foggy day
Kotlin - Kotlin casting, smart cast 본문
class KotlinPlayGroundActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kotlin_play_ground)
val message = "It is test"
val length: Int = if (message is String)
message.length
else
100
println("length : $length")
val value: Int = when (message) {
is String -> message.length
else -> 100
}
println("value : $value")
val rValue: String = message as String
println("rValue : $rValue")
val rValueSecond : String? = message as? String
println("rValueSecond : $rValueSecond")
}
}
'Kotlin' 카테고리의 다른 글
Kotlin - lambda expression -1 (0) | 2021.03.20 |
---|---|
Kotlin - trimMargin, multiple line string (0) | 2021.03.19 |
Kotlin - Elvis, null check (0) | 2021.03.17 |
Kotlin - anonymous function (0) | 2021.03.17 |
Kotlin - extension function, fold, reduce (0) | 2021.03.17 |