Kotlin
Kotlin Language
A JVM language developed by JetBrains. The official language for Android. 100% compatible with Java. Concise syntax.
Kotlin Language
A JVM language developed by JetBrains. The official language for Android. 100% compatible with Java. Concise syntax.
Kotlin is a modern programming language announced by JetBrains in 2011 and officially released in 2016. It runs on the JVM (Java Virtual Machine) and was designed to address Java's verbose syntax and null-related bugs. Created with the goal of being "a better Java," it is fully compatible with existing Java code.
It was announced as an officially supported language for Android at Google I/O in 2017, and in 2019 it was designated as the preferred language for Android development (Kotlin-first). Currently, more than 80% of new Android apps are written in Kotlin, and global companies such as Pinterest, Netflix, Uber, and Slack are actively adopting Kotlin.
Kotlin's key features are null safety (Null Safety), extension functions, data classes, and asynchronous programming based on coroutines (Coroutines). In particular, its type system, which prevents NullPointerException at compile time, fundamentally eliminates the most common runtime error in Java.
Kotlin supports not only the JVM but also Kotlin/JS, which transpiles to JavaScript, and Kotlin/Native, which generates native binaries. With Kotlin Multiplatform, business logic can be shared across Android, iOS, web, server, and desktop, opening a new paradigm for cross-platform development.
// 1. Null Safety - 컴파일 타임 null 체크
fun getUserName(user: User?): String {
// Elvis 연산자와 safe call
return user?.name ?: "익명 사용자"
}
// 2. 데이터 클래스 - 보일러플레이트 코드 제거
data class User(
val id: Long,
val name: String,
val email: String
)
// equals, hashCode, toString, copy 자동 생성
val user1 = User(1, "홍길동", "hong@example.com")
val user2 = user1.copy(name = "김철수")
// 3. 확장 함수 - 기존 클래스에 함수 추가
fun String.addExclamation(): String = "$this!"
println("안녕하세요".addExclamation()) // 출력: 안녕하세요!
// 4. 코루틴 - 비동기 프로그래밍
import kotlinx.coroutines.*
suspend fun fetchUserData(userId: Long): User {
return withContext(Dispatchers.IO) {
// 네트워크 요청 시뮬레이션
delay(1000)
User(userId, "사용자$userId", "user$userId@example.com")
}
}
fun main() = runBlocking {
val users = listOf(1L, 2L, 3L).map { id ->
async { fetchUserData(id) }
}.awaitAll()
users.forEach { println("${it.name}: ${it.email}") }
}
// 5. 컬렉션 처리 - 함수형 스타일
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val result = numbers
.filter { it % 2 == 0 } // 짝수: [2, 4, 6, 8, 10]
.map { it * it } // 제곱: [4, 16, 36, 64, 100]
.sum() // 합계: 220
println("짝수 제곱의 합: $result")
"We'll develop new Android apps in Kotlin. Thanks to null safety, we can cut NPE-related crashes by more than 90%, and coroutines let us write asynchronous code much more cleanly. We can also keep using existing Java libraries as they are."
"Kotlin's coroutines are lightweight threads, and with suspend functions you can write asynchronous code as if it were synchronous code. You control the execution context through a Dispatcher and manage coroutine lifecycles with structured concurrency. Compared to RxJava, the learning curve is lower and readability is better."
"You're using the !! operator (not-null assertion) here, which can cause an NPE at runtime. Please replace it with safe call (?.) and the Elvis operator (?:), or verify explicitly with the require/check functions."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
The !! operator forcibly asserts that a value is not null, but if it is null, an NPE occurs. Because it nullifies Kotlin's null safety, replace it with ?. or ?: whenever possible.
GlobalScope persists for the lifetime of the app and can cause memory leaks. Use scopes that match the lifecycle, such as ViewModel's viewModelScope or lifecycleScope.
equals, hashCode, toString, and copy methods are generated automatically, greatly reducing boilerplate. Make active use of them in data holder classes such as DTOs and Entities.