본문 바로가기

Kotlin 공부

kotlinlang.org 내가 보려고 정리 - Basic types

Basic types

코틀린의 데이터 타입을 추론할 수 있는데 이를 type inference라고 한다.

fun main() {
	var customers = 10
    // customers의 데이터 타입은 Int라고 추론된다.
    // 컴파일러는 customer로 산술연산을 할 수 있음을 안다.
    
    customers = 8
    
    customers = customers + 3 // result: 11
    customers += 7 // 18
    customers -= 3 // 15
    customers *= 2 // 30
    customers /= 3 // 10
    customers %= 7 // 3
    
    println(customers) // 3
}

 

Kotlin의 Basic types

Category Basic types
Integers Byte, Short, Int, Long
Unsigned integers UByte, UShort, UInt, ULong
Floating-point numbers Float, Double
Booleans Boolean
Characters Char
Strings String
fun main() {
    val d: Int
    /* 
    변수를 선언하고 나중에 초기화할 수 있다.
    변수를 처음 읽기 전에 변수를 초기화해주면 된다.
    변수를 초기화하지 않고 선언하려면 : 뒤에 타입을 명시해줘야 한다.
    */
	
    d = 3
    
    val e: String = "hello"
    //선언하면서 초기화할 때 타입을 명시해줘도 된다.
    
    println(d) // 3
    println(3) // hello
    
    // 변수를 초기화하기 전에 읽으려고 하면 에러 발생.
}