This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val list = listOf("a", "b", "c") | |
if (-1 !in 0..list.lastIndex) { | |
println("-1은 범위 밖에 있네요") | |
} | |
if (list.size !in list.indices) { | |
println("리스트 사이즈가 인덱스 범위 밖에 있네요") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val x = 10 | |
val y = 9 | |
if (x in 1..y+1) { | |
println("range 안에 들어가네요") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun describe(obj: Any): String = | |
when (obj) { | |
1 -> "One" | |
"Hello" -> "Greeting" | |
is Long -> "Long" | |
!is String -> "Not a string" | |
else -> "Unknown" | |
} | |
fun main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val items = listOf("apple", "banana", "kiwifruit") | |
var index = 0 | |
while (index < items.size) { | |
println("item at $index is ${items[index]}") | |
index++ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val items = listOf("apple", "banana", "kiwifruit") | |
for (item in items) { | |
println(item) | |
} | |
for (index in items.indices) { | |
println("인덱스 $index 번에 있는 항목은 ${items[index]} 입니다.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getStringLength(obj: Any): Int? { | |
if (obj !is String) return null | |
// obj는 자동으로 String으로 형변환됩니다. | |
return obj.length | |
} | |
fun getStringLength(obj: Any): Int? { | |
// obj는 && 오른쪽 구문부터 String으로 형변환됩니다. | |
if (obj is String && obj.length > 0) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getStringLength(obj: Any): Int? { | |
if (obj is String) { | |
// 블록에 접근하게 되면 String 타입으로 안전하게 형변환됨 | |
return obj.length | |
} | |
// 형변환되는 블록 밖에서는 Any 타입을 그대로 사용 | |
return null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun parseInt(str: String): Int? { | |
return str.toIntOrNull() | |
} | |
fun printProduct(arg1: String, arg2: String) { | |
val x = parseInt(arg1) | |
val y = parseInt(arg2) | |
if (x == null) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun parseInt(str: String): Int? { | |
return str.toIntOrNull() | |
} | |
fun printProduct(arg1: String, arg2: String) { | |
val x = parseInt(arg1) | |
val y = parseInt(arg2) | |
// x + y는 둘 중 null인 값을 가진 것이 있을 경우 에러가 날 수 있습니다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun parseInt(str: String): Int? { | |
// ... | |
} |