Introduction to Kotlin
What is Kotlin?
Kotlin is a general-purpose, statically-typed programming language created by JetBrains as a more concise and safer alternative to Java.
It runs on the Java Virtual Machine (JVM), so it can use any existing Java library, but it adds modern features like null safety, data classes, and extension functions.
kotlinc compiler runs plain .kt files from any terminal.Example: What is Kotlin?
// Entry point: execution of the program starts here
fun main() {
// Print "Kotlin is a modern programming language" to the console, with a trailing newline
println("Kotlin is a modern programming language")
// Print "It runs on the JVM and beyond" to the console, with a trailing newline
println("It runs on the JVM and beyond")
}
Login to try C/C++/Java/PHP code in the editor
Why Kotlin Was Created
JetBrains built Kotlin to fix pain points they experienced writing Java every day, such as verbose boilerplate and the risk of null pointer exceptions.
Kotlin keeps full interoperability with Java while adding safer defaults and much more concise syntax.
Example: Why Kotlin Was Created
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `goals`, set to `listOf("conciseness", "safety", "interoperability")`
val goals = listOf("conciseness", "safety", "interoperability")
// Loop over `goals`, binding each item to `goal`
for (goal in goals) {
// Print "Design goal: $goal" to the console, with a trailing newline
println("Design goal: $goal")
}
}
Login to try C/C++/Java/PHP code in the editor
Where Kotlin Is Used
Kotlin is the preferred language for Android app development, but it is also used for server-side backends (with frameworks like Ktor and Spring), for multiplatform mobile code shared between iOS and Android, and for general scripting.
Example: Where Kotlin Is Used
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `usedFor`, set to `listOf("Android apps" to "Google apps", "Backend" to "Ktor servers", "Scripting" to "build scripts")`
val usedFor = listOf("Android apps" to "Google apps", "Backend" to "Ktor servers", "Scripting" to "build scripts")
for ((area, example) in usedFor) {
// Print "$area: $example" to the console, with a trailing newline
println("$area: $example")
}
}
Login to try C/C++/Java/PHP code in the editor
Compiled on the JVM
Kotlin source code is compiled into JVM bytecode before it runs, just like Java.
This lets the compiler catch many mistakes, such as type errors, before your program ever executes, and it lets Kotlin call Java code and vice versa without any extra work.
kotlinc file.kt -include-runtime -d app.jar then java -jar app.jar compiles and runs in two quick steps.Example: Compiled on the JVM
// Entry point: execution of the program starts here
fun main() {
// Print "This code is compiled to JVM bytecode" to the console, with a trailing newline
println("This code is compiled to JVM bytecode")
// Print "The compiler already checked it for errors" to the console, with a trailing newline
println("The compiler already checked it for errors")
}
Login to try C/C++/Java/PHP code in the editor
- Assuming Kotlin is only for Android apps and cannot be used for backend, scripting, or plain command-line programs.
- Confusing Kotlin with Java; Kotlin is a separate, newer language with its own syntax even though it runs on the same JVM.
- Thinking Kotlin code always needs Android Studio to run, when a plain
.ktfile can be compiled and run from a terminal.
- Kotlin is a modern, statically-typed language created by JetBrains and released in 2011.
- It runs on the JVM, compiles to JavaScript, and compiles natively via Kotlin/Native.
- Google made Kotlin an officially supported language for Android development in 2017.
- Kotlin interoperates fully with Java, so existing Java libraries can be used directly from Kotlin code.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: