Language/Kotlin
                
              Kotlin - Characters (문자)
                TechNote.kr
                 2018. 7. 10. 22:56
              
              
                    
        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