Skip to content

Instantly share code, notes, and snippets.

@gyugyu90
Created April 26, 2020 11:21
Show Gist options
  • Save gyugyu90/d3b1127cefe42211b20d1a93abfa5b67 to your computer and use it in GitHub Desktop.
Save gyugyu90/d3b1127cefe42211b20d1a93abfa5b67 to your computer and use it in GitHub Desktop.
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인 값을 가진 것이 있을 경우 에러가 날 수 있습니다.
if (x != null && y != null) {
// x와 y의 null체크를 하고 null이 아닌 값들일 때만 이 블록에 들어오게 됩니다.
println(x + y)
} else {
println("'$arg1' or '$arg2' is not a number")
}
}
fun main() {
printProduct("6", "7")
printProduct("a", "7")
printProduct("a", "b")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment