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.
Note: You don't need Android Studio to try Kotlin -- the standalone kotlinc compiler runs plain .kt files from any terminal.
Example: What is Kotlin?
fun main() {
println("Kotlin is a modern programming language")
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
fun main() {
val goals = listOf("conciseness", "safety", "interoperability")
for (goal in goals) {
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
fun main() {
val usedFor = listOf("Android apps" to "Google apps", "Backend" to "Ktor servers", "Scripting" to "build scripts")
for ((area, example) in usedFor) {
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.
Note: Running 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
fun main() {
println("This code is compiled to JVM bytecode")
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: