Kotlin print() – Print without New Line
To print without a new line in Kotlin, use print() statement. print() prints the message to standard output console.
In this tutorial, we will learn how to use print() with different examples demonstrating different scenarios.
Example 1 – Kotlin print() – String message
In this example, we shall print a string to the standard output console using print().
Kotlin Program – example.kt
/**
* Kotlin - print() - Print without New Line
*/
fun main(args: Array<String>) {
var str = "Hello World"
print(str)
print(str)
}
Run the above Kotlin program, and you shall see the following output print to standard console output.
Output
Hello WorldHello World
Please note that, after printing the message “Hello World”, the cursor waits right after the message. When another print() statement is executed, the message is printed from the current cursor position. So, the messages are printed right after one another.
Example – Kotlin print() – Other Datatypes
In this example, we shall print messages of different datatypes to the standard output console using print() statement.
Kotlin Program – example.kt
/**
* Kotlin - print() - Print without New Line
*/
fun main(args: Array<String>) {
print("Hello World") //string
print(35) //int
print(12345L) //long
print(0b00001011) //byte
print(2.35) //double
print(41.253F) //float
print('m') //Char
print(true) //Boolean
}
Run the above Kotlin program, and you shall see the following output print to standard output.
Output
Hello World3512345112.3541.253mtrue
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. And all the messages are printed one after another in a single line.
Note: Unlike println() statement, you cannot call print() statement without any argument.
Conclusion
In this Kotlin Tutorial, we learned how to print a message without new line to the standard output.