1. Getting to know the println() command
You have just written your first Kotlin program and printed your first message to the screen. Congratulations. Now let’s not stop there...
Any Kotlin program consists of commands. Commands can be different. The Kotlin language has commands for every occasion. Each command describes some specific action.
In Kotlin (unlike Java) a semicolon is usually not needed: most often, each command is written on a new line. A semicolon is useful if you want to write multiple commands on one line (but people usually don’t do that).
Examples of commands:
| Command | Description (what it does) |
|---|---|
|
Prints the number to the screen: |
|
Prints the text to the screen: |
|
Prints the text to the screen: |
In fact, this is a single command — println(). And in the parentheses, parameters are passed to it. Depending on the parameter values, one command can perform different actions. That’s very convenient.
Letter case matters
In Kotlin, case matters: it’s important whether commands are written in uppercase or lowercase letters. The command println() will work, but Println() will not. If you write a command with the wrong letter case, Kotlin won’t recognize it and therefore won’t execute it. So be careful.
Double quotes
If you want to print text to the screen, you need to enclose it on both sides with double quotes.
A single quote looks like this ', and a double quote looks like this ". A double quote is not two single quotes—please don’t confuse them.
Double quote is the one next to the Enter key. On many keyboards it also shares a key with the Cyrillic letter "E".
2. Getting to know the print() command
There is one more command similar to println(), and it is called print().
The println() command prints text to the screen and then moves the cursor to the next line, as if pressing Enter. As a result, the next println() command will print its text on the next line. And the next command—on the following line, and so on.
Unlike println(), the print() command does not add Enter after printing. Therefore, text printed by several print() commands will appear on a single line.
Example 1:
print("Amigo")
println("The")
print("Best")
Will print to the screen:
AmigoThe
Best
Example 2:
print("Amigo")
print("The")
print("Best")
Will print to the screen:
AmigoTheBest
If you want to print text on one line, use print(); if on multiple lines, use println(). Simple.
3. Breaking down the println() command
If you know at least a little English, learning programming will be much easier. The names of many commands are directly related to what they do.
For example, println is print + line — print a line. I think everyone knows the word print, and why it’s specifically a “line” I’ll explain now. Each row of text on the screen is called a line. So printing a line is exactly outputting a line of text.
Printing text to the screen is usually called output to the console. The console is one of the most common ways for a program and a person to interact. When a user types commands to a program on the keyboard, and the program responds by printing text to the screen, programmers say that the program works with the console.
In other words, working with the console means printing text to the screen and entering data using the keyboard. So if in the future you hear that a program output something to the console, it just means it printed text to the screen.
That’s why println() is basically: “Hey, console, print me a line of text.”
4. Examples of printing different data
Let’s play around with console output. We’ll start with the simplest case and gradually make it more complex:
Printing numbers
println(1001)
println(-15)
Printing text
println("Programming is awesome!")
println("Just kidding—now let's code.")
Printing multiple values at once
Trick: you can add strings together using +. Though it’s better to call it concatenation:
println("My age: " + 28)
Will print to the screen:
My age: 28
You can also concatenate multiple strings in a row:
println("It is now the year " + 2026 + ".")
Will print to the screen:
It is now the year 2026.
5. Common mistakes when using println()
Young Jedi often step on the same rake. But so that you don’t turn into the hero of the book “1001 Kotlin mistakes,” here’s a list of common slip-ups and tips on how to avoid them.
Mistake 1. Forgot to close the string—lost the second quote:
println("Oops, you opened the quote and forgot to close it)
The compiler will complain here: "unclosed string literal" — close your strings!
Mistake 2. Writing two commands on one line without a separator
In Kotlin, the usual style is one command = one line. If you wrote two commands on one line, you need either a newline between them or a ;.
You can’t do this:
println("No separator") println("between commands")
But this is allowed (though it’s usually easier to move it to a new line):
println("There is a separator"); println("between commands")
Mistake 3. Using the wrong quotes
Beginners sometimes use angle quotes or single quotes:
println(‘Error’) // wrong!
println('Error') // also wrong!
For strings, always use standard double quotes—like this: ".
GO TO FULL VERSION