Kotlin println() – Print with a New Line
To print with a new line in Kotlin, use println() statement. println() prints a new line after it prints its argument.
In this tutorial, we will learn how to use println() with different examples demonstrating different scenarios.
Example 1 – Kotlin println() – String message
In this example, we shall print a string to the standard output console.
Kotlin Program – example.kt
/**
* Kotlin - println() - Print with New Line
*/
fun main(args: Array<String>) {
var str = "Hello World"
println(str)
println(str)
}
Run the above Kotlin program, and you shall see the following output print to standard console output.
Output
Hello World
Hello World
Please note that, after the message “Hello World”, a new line is printed to the standard output. Then we have printed the message “Hello World” again using println(). A new line is printed after the second message also.
Example 2 – Kotlin println() – Other Datatypes
In this example, we shall print messages of different datatypes to the standard output console.
Kotlin Program – example.kt
/**
* Kotlin - println() - Print with New Line
*/
fun main(args: Array<String>) {
println("Hello World") //string
println(35) //int
println(12345L) //long
println(0b00001011) //byte
println(2.35) //double
println(41.253F) //float
println('m') //Char
println(true) //Boolean
println() //no argument
}
Run the above Kotlin program, and you shall see the following output print to standard output.
Output
Hello World
35
12345
11
2.35
41.253
m
true
We can see that everything kind of printed as we have mentioned, but without the suffixes or such that differentiate the datatype from others: like Long. Also, the byte has been converted to int and printed to the console.
Conclusion
In this Kotlin Tutorial, we learned how to print a message, and new line separator, to the standard output.