Kotlin:
Using a debugger
How to:
Here’s a little taste of debugging in Kotlin with IntelliJ IDEA - the Sherlock Holmes of IDEs:
fun main() {
val mysteryNumber = 42
var guess = 0
while (guess != mysteryNumber) {
println("Guess the number: ")
guess = readLine()?.toIntOrNull() ?: continue // Ignore bad inputs
// Set a breakpoint here to watch 'guess' in action
if (guess < mysteryNumber) {
println("Too low!")
} else if (guess > mysteryNumber) {
println("Too high!")
}
}
println("You've got it! The mystery number was $mysteryNumber")
}
Debugger output:
Guess the number:
10
Too low!
Guess the number:
50
Too high!
Guess the number:
42
You've got it! The mystery number was 42
Deep Dive
Debuggers have been in the game since the ’50s. Back then, they were pretty primitive, and debugging could be more about hardware than software. Nowadays, a debugger like the one in IntelliJ IDEA lets us set breakpoints, step through code line by line, and inspect the state of variables at our leisure.
While IntelliJ’s debugger is super handy for Kotlin, it’s not the only fish in the sea. There’s a range of alternatives like Logcat for Android development, or command-line tools like jdb for the minimalists. The under-the-hood magic here is mostly about JVM Tool Interface (JVMTI), which lets debuggers interact with the Java Virtual Machine, keeping Kotlin developers in the loop.
See Also
- IntelliJ IDEA Debugger documentation: https://jetbrains.com/idea/