CodeGym /Courses /Kotlin SELF /The Kotlin compiler and comments

The Kotlin compiler and comments

Kotlin SELF
Level 1, Lesson 5
Available

1. Getting to know the compiler

A computer doesn’t understand human language. A computer doesn’t even understand programming languages. It only understands programs written in machine code—made up of zeros and ones. People think in words and ideas, and of course, it’s hard for them to compose entire programs out of nothing but 0 and 1.

00100100 translates to the decimal number 36 or the symbol $

That’s why programming languages like Kotlin appeared. They became a kind of “translator”: now a programmer can write commands like “Do this and that” in a language they understand, rather than as a long chain of commands for the CPU.

But there’s a catch. The computer still doesn’t understand Kotlin! If you just feed it a file with code, it will shrug and do nothing. That’s exactly why the compiler exists.

So what is a compiler?

A compiler is a special program that compiles code: it translates Kotlin code into code the computer can understand. It takes text in Kotlin, parses it, checks it for errors, and turns it into bytecode for the JVM virtual machine.

        flowchart TD
    A[📝 Kotlin code] --> B[Compiler]

    B --> C{Any errors?}

    C -->|Yes ❌| H[Error message Return to code]
    H --> A

    C -->|No ✅| I[⚙️ Bytecode generation]

    I --> J[JVM bytecode]

    J --> K[JVM virtual machine]

    K --> L[Program execution]

    style A fill:#7B68EE,color:#fff
    style B fill:#FF8C00,color:#fff
    style J fill:#20B2AA,color:#fff
    style K fill:#3CB371,color:#fff
    style L fill:#3CB371,color:#fff
    style H fill:#DC143C,color:#fff
    
This is a simplified diagram to convey the general idea.

You can imagine the compiler as a strict teacher who:

  • carefully checks phrases (did you make a mistake?),
  • forces you to follow the rules (for example, proper variable names, brackets, types...),
  • and only then allows you to “interview” with the computer!

Without a compiler, writing a program would be like composing “War and Peace” in Morse code.

The compiler is even smarter

The Kotlin compiler has gotten even smarter—it can not only translate, but also optimize code, point out mistakes, and even suggest how to write better. And many, many other things.

If we talk about Kotlin on the JVM (that is, regular console apps/backend on the JVM), it usually works like this:

  • At the first stage, your Kotlin code is transformed into special intermediate JVM bytecode.
  • And then at runtime, the JVM (virtual machine) translates that bytecode into real CPU machine instructions (and can do so taking the specific machine into account).
  • A program in Kotlin
  • Kotlin compiler
  • A program consisting of special codes (bytecode)
  • Java VM
  • A program consisting of machine code
Compilation stages for Kotlin

If you have a powerful CPU that supports a larger set of machine instructions, then during such translation/optimizations the JVM can generate machine code that takes advantage of your CPU and OS. That’s why programs on JVM languages (including Kotlin) can be very fast for some tasks.

Not only the JVM

Originally, Kotlin was created primarily as a language for the JVM (that is, “like Java, but more convenient”). So for a long time the main scenario was: Kotlin code → JVM bytecode → execution in the JVM.

But later Kotlin got other “compilation modes” (other run scenarios):

  • Kotlin/JVM — the most classic option: console, server, Android (partly), everything in the JVM world.
  • Kotlin/JS — compilation to JavaScript for the browser or Node.js.
  • Kotlin/Native — compilation to native machine code (for example, for iOS, macOS, Windows, Linux).
  • Kotlin Multiplatform — an approach where part of the code is shared and part is platform-specific, and the project can be built for several targets at once.

The idea stays the same: the compiler takes Kotlin code and turns it into a format the target platform “understands”—the JVM, the browser (JS), or a native OS.

2. Typical compilation errors

Code speed and optimization aren’t very important to us yet, but what is important is that the compiler checks your program for errors. It checks the code for correctness and won’t let it go further if it finds even a small mistake.

Error example:

You try to assign a number to a variable that can store only text.

val userName: String = 42 // Oops! You can’t do that.

The compiler will immediately produce an error that the types don’t match (something like): Type mismatch: inferred type is Int but String was expected.

Another example is a typo in the println command:

printline("Hello!") // Error in function name

The compiler will say something like: “No such function—check your syntax!” (for example, Unresolved reference: printline).

How do you read errors?

Most compilers tell you where in the code the error is, and sometimes even suggest fixes. Don’t be afraid of errors: each one is a step toward enlightenment and defeating your inner “boss.”

3. Comments

Reading the code of that one guy who claimed: “My code doesn’t need comments; it’s clear as is.”

So imagine this: you write a complex program. Everything works, you’re a genius! But a month later you open that same code and... you don’t understand anything. A couple lines of code here, a weird variable there. In your head there’s only: “Someone clearly wrote this—but definitely not me!” That’s where comments save you.

Comments are like notes in the margins of a textbook or labels on the fridge (“Beer. Don’t drink. For the morning!”). The compiler ignores them, and people do the opposite. Comments let you explain the meaning of code, mark important details, and even “turn off” parts of programs without deleting them.

Important: the compiler does not read your comments; they are completely ignored!

Single-line comments

In Kotlin (as in Java) there are two types of comments—single-line and multi-line. Single-line comments start with a double slash // and go to the end of the line. Everything after // is considered a comment and is absolutely ignored by the compiler.

Example:

println("Hello, world!") 	// This prints a greeting to the screen

Here // This prints a greeting to the screen is a comment. If in the code it seems unclear why a line is needed, write it like this!

Another example:

val x = 42 	// The programmer’s age value

You can also write single-line comments on their own lines:

// Initializing a variable with the user name
val userName = "Alice"

Almost like lines in a script: the comment explains what and why.

Multi-line comments

What if you have a lot to say? A real story? That’s what multi-line comments are for: they start with /* and end with */. Everything between these symbols is ignored by the compiler, even if it spans multiple lines.

Example:

/*
  Here we display the user name on the screen.
  If the name is missing, we’ll print "Guest".
*/
println("Enter your name:")

Or like this:

var age = 18 /* Initial age value
                for a new user */

Attention: you can’t nest multi-line comments inside other multi-line comments. Don’t try to outsmart the compiler—it won’t work.

Illustration:

Type Syntax Example
Single-line
// comment
val x = 1 // explanation
Multi-line
/* stuff here */
/* here you can explain anything you want */

4. Real-life examples

“Disabling” a line of code:

Sometimes you temporarily don’t want to execute a fragment of code. Comments are perfect for that:

// println("Text that is temporarily not needed")

The line is still there, but the compiler doesn’t “see” it.

TODO and FIXME notes

Programmers are people too, and sometimes you need to remind yourself that code isn’t finished yet, or that there’s a bug here. For this, people often write:

// TODO: add validation for empty input
// FIXME: this function calculates the sum incorrectly

Many editors and IDEs even highlight such words!

Programmers joke

// I am not responsible for this code. I was forced to write it against my will.
// Dear me_from_the_future! Please forgive me for this code.
// If I see this again, I’ll have to start bringing a weapon to work.
// If this condition ever becomes true, please contact me at tel. xxx-xxx-xxx for a reward.
// Dear programmer:
//
// When you finish “optimizing” this subroutine
// and realize how big a mistake it was to do this,
// please increase the counter below as a warning
// for the next guy:
//
// hours_wasted_here = 42
// When I started writing this, only God and I understood what I was doing.
// Now only God remains
// Sometimes it seems to me that the compiler ignores all my comments
// I dedicate all my code, all my work to my wife Darlene, who
// will have to support me, our three kids, and the dog when
// this goes to production.

Yes, comments can be very funny. After all, they’re written by real people.

1
Task
Kotlin SELF, level 1, lesson 5
Locked
Comments next to the output
Comments next to the output
1
Task
Kotlin SELF, level 1, lesson 5
Locked
The Missing println
The Missing println
1
Task
Kotlin SELF, level 1, lesson 5
Locked
Disable debugging
Disable debugging
1
Task
Kotlin SELF, level 1, lesson 5
Locked
Cascade of failures
Cascade of failures
1
Survey/quiz
Introduction to Kotlin, level 1, lesson 5
Unavailable
Introduction to Kotlin
Commands and the first program in Kotlin
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION