본문으로 건너뛰기

기본 자료형

Kotlin의 모든 변수와 데이터 구조에는 유형이 있습니다. 유형은 컴파일러에게 해당 변수 또는 데이터 구조로 무엇을 할 수 있는지 알려주기 때문에 중요합니다. 즉, 어떤 함수와 속성을 가지고 있는지 알려줍니다.

지난 장에서 Kotlin은 이전 예제에서 customers의 유형이 Int임을 알 수 있었습니다. 유형을 추론하는 Kotlin의 능력을 **타입 추론 (type inference)**이라고 합니다. customers에는 정수 값이 할당됩니다. 여기에서 Kotlin은 customers가 숫자 유형 Int를 갖는다고 추론합니다. 결과적으로 컴파일러는 customers로 산술 연산을 수행할 수 있음을 알고 있습니다.

fun main() {

var customers = 10

// Some customers leave the queue
customers = 8

customers = customers + 3 // Example of addition: 11
customers += 7 // Example of addition: 18
customers -= 3 // Example of subtraction: 15
customers *= 2 // Example of multiplication: 30
customers /= 3 // Example of division: 10

println(customers) // 10

}

+=, -=, *=, /=, 및 %=는 augmented assignment operators (확대 할당 연산자)입니다. 자세한 내용은 Augmented assignments를 참조하세요.

전반적으로 Kotlin에는 다음과 같은 기본 유형이 있습니다.

CategoryBasic typesExample code
Integers (정수)Byte, Short, Int, Longval year: Int = 2020
Unsigned integers (부호 없는 정수)UByte, UShort, UInt, ULongval score: UInt = 100u
Floating-point numbers (부동 소수점 숫자)Float, Doubleval currentTemp: Float = 24.5f, val price: Double = 19.99
Booleans (부울)Booleanval isEnabled: Boolean = true
Characters (문자)Charval separator: Char = ','
Strings (문자열)Stringval message: String = "Hello, world!"

기본 유형 및 해당 속성에 대한 자세한 내용은 Basic types를 참조하세요.

이 지식을 바탕으로 변수를 선언하고 나중에 초기화할 수 있습니다. Kotlin은 변수가 처음 읽기 전에 초기화되는 한 이를 관리할 수 있습니다.

초기화하지 않고 변수를 선언하려면 :로 유형을 지정합니다. 예시:

fun main() {

// Variable declared without initialization
val d: Int
// Variable initialized
d = 3

// Variable explicitly typed and initialized
val e: String = "hello"

// Variables can be read because they have been initialized
println(d) // 3
println(e) // hello

}

변수를 읽기 전에 초기화하지 않으면 오류가 표시됩니다.

fun main() {

// Variable declared without initialization
val d: Int

// Triggers an error
println(d)
// Variable 'd' must be initialized

}

이제 기본 유형을 선언하는 방법을 알았으니 collections에 대해 알아볼 시간입니다.

Practice

Exercise

각 변수에 대해 올바른 유형을 명시적으로 선언하십시오.

|---|---|

fun main() {
val a: Int = 1000
val b = "log message"
val c = 3.14
val d = 100_000_000_000_000
val e = false
val f = '
'
}

|---|---|

fun main() {
val a: Int = 1000
val b: String = "log message"
val c: Double = 3.14
val d: Long = 100_000_000_000_000
val e: Boolean = false
val f: Char = '
'
}

Next step

Collections