참고
코틀린 기초
NOTE
변수
NOTE
기본적인 연산은 자바와 거의 동일하므로 크게 설명하지 않는다.
// 변수 선언
val answer = 42 // val - immutable(수정불가)
var answer : Int = 42 // var - mutable(수정가능)
// 초기화하지 않으면 변수명시 필수
val answer: Int
answer = 42
// 변수출력
println("There are $customers customers")
println("There are ${customers + 1} customers")
// 변수연산
customers = customers + 3
customers += 7
customers -= 3
customers *= 2
customers /= 3
Kotlin
복사
val(수정 불가), var(수정 가능), 문자열 출력 코드
Collections(List, Set, Map)
NOTE
// 수정 x, 읽기 o
val readOnlyShapes : Set<String> = listOf("triangle", "square", "circle")
println("The first item in the list is: ${readOnlyShapes[0]}") // [] 접근
println("The first item in the list is: ${readOnlyShapes.first()}") // first, last
println("This list has ${readOnlyShapes.count()} items") // 목록 수
println("circle" in readOnlyShapes) // in (전체 순환)
// 수정 o, 읽기 o
val shapes : MutableList<String> = mutableListOf("triangle", "square", "circle")
shapes.add("pentagon")
shapes.remove("pentagon")
// 읽기전용 수정
val shapesLocked : List<String> = shapes
Kotlin
복사
list 타입
val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
println(readOnyFruit) // [apple, banana, cherry]
println("This set has ${readOnlyFruit.count()} items") // 3
println("banana" in readOnlyFruit) // true
var fruit : MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
fruit.add("dragonfruit")
fruit.remove("dragonfruit")
Kotlin
복사
set 타입
val readOnlyJuiceMenu : Map<String, Int> =
mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println("The value of apple juice is: ${readOnlyJuiceMenu["apple"]}") // 100
println("This map has ${readOnlyJuiceMenu.count()} key-value pairs") // 3
println(readOnlyJuiceMenu.containsKey("kiwi")) // true
println(readOnlyJuiceMenu.keys) // apple kiwi orange
println(readOnlyJuiceMenu.values) // 100 190 100
println("orange" in readOnlyJuiceMenu.keys) // containesKey하고 같은거 아닌가?
println(200 in readOnlyJuiceMenu.values) // 200값이 있는가? false
juiceMenu : Mutable<String, Int> =
mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
juiceMenu.put("coconut", 150)
juiceMenu.remove("orange")
Kotlin
복사
조건 / 반복문
NOTE
val d : Int
val check = true
if(check) {
d = 1
} else {
d = 2
}
val a = 1
val b = 2
println(if (a>b) a else b) // 2
Kotlin
복사
if-else 조건
val obj = "Hello"
when (obj) {
"1" -> println("One")
"Hello" -> println("Greeting") // 출력
else -> println("Unknown")
}
val temp = 18
val description = when {
temp < 0 -> "very cold"
temp < 10 -> "a bit cold"
temp < 20 -> "warm"
else -> "hot"
}
println(description)
Kotlin
복사
when 조건
// 1..4 => 1~4
// 1..<4 => 1~3
// 4 downTo => 4~1
// 1..5 step2 => 1,3,5
for (number in 1..5) {
print(number)
}
// 12345
val cakes = listOf("carrot", "cheese", "chocolate")
for (cake in cakes) {
println("Yummy, it's a $cake cake!")
}
// Yummy, it's a carrot cake!
// Yummy, it's a cheese cake!
// Yummy, it's a chocolate cake!
Kotlin
복사
// 기본 while문
var cakesEaten = 0
while (cakesEaten < 3) {
println("Eat a cake")
cakesEaten++
}
// do-while문
var cakesEaten = 0
var cakesBaked = 0
while (cakesEaten < 3) {
println("Eat a cake")
cakesEaten++
}
do {
println("Bake a cake")
cakesBaked++
} while (cakesBaked < cakesEaten)
Kotlin
복사
함수
NOTE
•
•
클래스
NOTE
•
•
널 체크
NOTE
•
•