728x90
문자는 Char type으로 표현된다.
fun check(c: Char) {
if (c == 1) { // ERROR: incompatible types
// ...
}
}
Character literal은 따옴표로 표시한다. 'a'
특수 문자들은 여타 다른 언어들과 마찬가지로 backslash를 가지고 표현한다.
\t, \b, \n, \r, \', \", \\, \$
일반 ascii 가 아닌 여타 다른 character로 encode 하기 위해서 Unicode escape sequence syntax를 사용한다.
character type을 Int type으로 변환 시킬 수 있는 경우도 있다.
fun decimalDigitValue(c: Char): Int {
if (c !in '0'..'9')
throw IllegalArgumentException("Out of range")
return c.toInt() - '0'.toInt() // Explicit conversions to numbers
}
Numbers 에서 언급했던 것과 같이 Char는 숫자로 표시되지 않는다. 만약 Char type으로 선언한 변수에 숫자를 할당하려고 하면 다음과 같은 error를 발생시킨다.
fun main(args: Array<string>) {
val value: Char = 66
println("$value")
}
Error:(2, 23) Kotlin: The integer literal does not conform to the expected type Char |
728x90
'Language > Kotlin' 카테고리의 다른 글
| Kotlin - Packages (0) | 2018.07.16 |
|---|---|
| Kotlin - Kotlin on JVM (the IntelliJ IDEA Community Edition for Windows) (0) | 2018.07.12 |
| Kotlin - Strings (문자열) (0) | 2018.07.11 |
| Kotlin - Arrays (배열) (0) | 2018.07.11 |
| Kotlin - Booleans (0) | 2018.07.11 |
| Kotlin - Numbers (숫자) (0) | 2018.07.10 |
| Kotlin - 기본 자료형, 그리고 변수의 선언 및 할당 (0) | 2018.07.09 |
| Kotlin - Kotlin 이 가지는 의미. (0) | 2018.07.09 |