What printing is
A program does most of its work out of sight. To see a value, a result or a message while the program runs, you print it to the console, the panel where a program's text output appears. Whatever a program prints is called its output. Printing is the first tool for checking what your code is doing, and every other lesson in this course uses it.
println: print a value and move to the next line
In Kotlin, the standard way to print is the function println. You put the value to print between its parentheses:
println(13)
The console shows:
13
Numbers don't need quotes. Text does, and it is covered in the next lesson. The difference matters: println(1998) prints a number, and println("1998") prints the four characters 1998 as text. On the screen they look the same, but they are different values to the program. For example, println(12 + 1) prints 13, while println("12" + 1) prints 121, because adding to text joins the pieces together instead of doing arithmetic.
The newline character
The ln at the end of println stands for line. After the value, println prints an invisible newline character, which does what pressing Enter does in a text editor: the next output starts on a new line. The newline belongs to the end of the printed line. It doesn't add an empty line after it.
That's why several println calls put their values on separate lines:
println(1)
println(2)
println(3)
The output is:
1
2
3
print: no newline
Kotlin also has print, which prints the value and stops there. Whatever is printed next continues on the same line:
print("a")
print("b")
println()
println("c")
The output is:
ab
c
Calling println() with nothing between the parentheses prints only a newline. Here it ends the line that the two print calls started, and calling it before a println would give you a blank line.
Printing text and several things at once
Text goes in double quotes, and the quotes are not printed. Inside a string, \n is an escape sequence that stands for a newline character, so one println can produce two lines:
println("Line1\nLine2")
Line1
Line2
To mix text and values in one line, put a $ before a variable's name inside the string, and Kotlin inserts the variable's value. This is called a string template. With a variable name holding "Andy":
val name = "Andy"
println("Hi, $name!")
Hi, Andy!
Variables get their own lesson later in this chapter, so treat this as a preview. The point for now is that one println call prints one line, and you decide what goes on it.
What you can print
println accepts values of any type and prints a text version of them:
println(3.14)
println(true)
println('x')
println(12 + 1)
The output is:
3.14
true
x
13
An expression such as 12 + 1 is evaluated first, and the result is printed.
Common mistakes
- Quoting a number you want to calculate with.
"1998"is text,1998is a number. - Forgetting the parentheses.
println 13doesn't compile. A function is called with parentheses after its name. - Capital letters. Kotlin is case-sensitive.
Println(13)fails withunresolved reference 'Println', because no function has that name. - Expecting
printto start a new line. Consecutiveprintcalls produce one long line. - Printing an array.
println(intArrayOf(1, 2))doesn't print the elements. It prints a type-and-hash text such as[I@6ea6d14e. UseintArrayOf(1, 2).toList()to see[1, 2]. A list can be printed directly.
The exercise
The lesson asks you to print the number 1998. There is one step: call println with the number between the parentheses, with no quotes, since it's a number.
println(1998)
Run it, and the console shows 1998.
Key takeaways
- The console shows a program's output, and printing is how you look at values while the program runs.
println(value)prints the value and then a newline, so the next output starts on a new line.print(value)prints without the newline, andprintln()alone prints just a newline.- Numbers are written without quotes, and text is written in double quotes.
- Function names are case-sensitive, and calls need parentheses.