JavaRush/Courses/Kotlin SELF/Output to the screen in Kotlin: println() and print()

Output to the screen in Kotlin: println() and print()

Open

1. Getting to know the println() command

You have just written your first Kotlin program and printed your first message to the screen. Congratulations on that. Let’s not stop there...

Any Kotlin program consists of commands. Commands can be different. The Kotlin language has commands for all occasions. 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 comes in handy if you want to write several commands on one line (but people usually don’t do that).

Examples of commands:

Command Description (what it does)
println(2026)
Prints the number to the screen:
2026
println("Ciao 🍕")
Prints the text to the screen:
Ciao 🍕
println("こんにちは 🗾")
Prints the text to the screen:
こんにちは 🗾

In fact, it’s 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 letters, 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 wrap 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 is the one next to the Enter key. It usually also has the Cyrillic letter "E" on it.

2. Getting to know the print() command

There is one more command similar to println(), and it’s called print().

The println() command prints text to the screen and then moves the cursor to the next line, as if pressing Enter. This way, the next println() command will print text on the next line. And the next command — on the next one, and so on.

Unlike println(), the print() command does not add Enter after printing text. Therefore, text printed by several print() commands will be printed on one line.

Example 1:

print("Amigo")
println("The")
print("Best")
Mixed output: print and println

Will print to the screen:

AmigoThe
Best

Example 2:

print("Amigo")
print("The")
print("Best")
Only print — everything on one line

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 for you. 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 as for why it’s a “line,” I’ll explain now. Each line 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 printing to the console. The console is one of the most common ways for a program and a person to interact. When the user types commands to the program on the keyboard and it prints text back 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 printed something to the console, it simply means it printed text to the screen.

So println() is basically: “Hey, console, print me a line of text.”

4. Examples of printing different types of data

Let’s play around with console output. We’ll start with the simplest and gradually make it more complex:

Printing numbers

println(1001)
println(-15)
Printing numbers

Printing text

println("Programming is cool!")
println("Just kidding — now let’s code.")
Printing strings

Printing multiple values at once

Trick: strings can be added using +. Although it’s better to call it concatenation:

println("My age: " + 28)
Concatenating a string and a number

Will print to the screen:

My age: 28

You can also concatenate several strings in a row:

println("Right now it is " + 2026 + ".")
Concatenating several values

Will print to the screen:

Right now it is 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 a book called “1001 Kotlin mistakes,” here’s a list of common blunders and tips on how to avoid them.

Mistake 1. Forgot to close the string — lost the second quote:

println("Oops, opened a quote and forgot to close it)

The compiler will complain: "unclosed string literal" — close your strings!

Mistake 2. Writing two commands on one line without a separator

In Kotlin, people usually write one command = one line. If you wrote two commands on one line, you need either a line break between them or a ;.

You can’t do this:

println("No separator") println("between commands")

But you can do this (although it’s usually easier to move to a new line):

println("There is a separator"); println("between commands")

Mistake 3. Using the wrong quotes

Beginners sometimes use guillemets or single quotes:

println(‘Error’)   // incorrect!
println('Error')   // also incorrect!

For strings, always use standard double quotes — like this: ".

1
Task
Kotlin SELF,  1 level,  2 lecture
Locked
Program interaction
Program interaction
1
Task
Kotlin SELF,  1 level,  2 lecture
Locked
One line in two parts
One line in two parts
1
Task
Kotlin SELF,  1 level,  2 lecture
Locked
Concatenation and line break
Concatenation and line break
1
Task
Kotlin SELF,  1 level,  2 lecture
Locked
Console Postcard
Console Postcard
Комментарии
  • популярные
  • новые
  • старые
Для того, чтобы оставить комментарий Вы должны sign in
This page has no comments yet