Kotlin Syntax and Language Basics
Kotlin has null-safety, type inference, data classes, coroutines, and a more concise syntax. Java is more verbose but has wider legacy support.
Use
Kotlin is a statically typed language, meaning type checking is done at compile time. However, it supports type inference to reduce verbosity.
Yes, Kotlin supports default parameter values:What are the main differences between Kotlin and Java?
How do I declare a variable in Kotlin?
val for immutable and var for mutable variables.
Example:
val name = "Alice"
var age = 30
What is the difference between
val and var?val is like a final variable in Java (read-only). var can be reassigned.
Is Kotlin statically or dynamically typed?
Can I use default arguments in Kotlin functions?
fun greet(name: String = "Guest") { println("Hello, $name!") }
Collections and Data Structures
Kotlin wraps Java collections but adds null-safety, immutability via
Use
Yes, Kotlin is interoperable with Java, so you can use
No, standard Kotlin collections are not thread-safe. Use concurrent collections from Java or Kotlinx libraries for thread safety.
What are the key differences between Java and Kotlin data structures?
List, Map, Set, and has extension functions for manipulation. Java lacks many of these features directly.
How do I create an immutable list in Kotlin?
listOf() to create an immutable list:
val names = listOf("Alice", "Bob")
Can I use Java collections in Kotlin?
ArrayList, HashMap, etc., but be careful with nullability.
What is the difference between mutableListOf and listOf?
mutableListOf() creates a list that can be modified (add/remove items), while listOf() creates an immutable list.
Are Kotlin collections thread-safe?
Null Safety and Type System
Kotlin distinguishes between nullable and non-nullable types.
You can use safe call
The
Kotlin uses type inference to deduce variable or return types when not explicitly specified. This keeps code concise and readable.
What is Kotlin's null safety feature?
String vs String?. This helps catch null pointer exceptions at compile time.
How do I handle nulls in Kotlin?
?., the Elvis operator ?:, or !! to assert non-null.
Example:
val length = name?.length ?: 0
What is the !! operator in Kotlin?
!! operator forces a nullable variable to be non-null. If it is null, it throws a NullPointerException. Use it with caution.
How does Kotlin infer types?
Coroutines and Asynchronous Programming
Coroutines provide a way to write asynchronous, non-blocking code. They are lightweight threads managed by the Kotlin runtime.
You can use
Yes, Kotlin coroutines work on JVM, Android, JS, and Native platforms. Just add the appropriate coroutine library.
What are coroutines in Kotlin?
How do I launch a coroutine?
GlobalScope.launch { } or use structured concurrency with lifecycleScope or viewModelScope in Android.
What's the difference between launch and async?
launch returns Job and does not return a result. async returns Deferred, which you can await() to get a result.
Can I use coroutines in plain Kotlin/JVM (non-Android)?
Interoperability and Compatibility
Yes, you can call Kotlin from Java and vice versa. Kotlin uses annotations like
Yes, with frameworks like Ktor or Spring Boot (which supports Kotlin), you can write full-stack apps in Kotlin.
Yes. Kotlin supports calling Java code using annotations and generics, though sometimes type casts or nullability handling are required.
Android Studio has a built-in tool: Paste Java code into a Kotlin file, and it will auto-convert. Or use Is Kotlin fully interoperable with Java?
@JvmStatic, @JvmOverloads, etc., to enhance compatibility.
Can Kotlin be used for backend development?
Can Kotlin call Java code with annotations or generics?
How do I convert Java code to Kotlin?
Code > Convert Java File to Kotlin File.