How to Use println in Kotlin: Simple Guide
In Kotlin, you use
println() to print text or values to the console. Just put what you want to show inside the parentheses, like println("Hello"), and it will display on a new line.Syntax
The println() function prints the given message and moves to the next line. You put the message inside parentheses and quotes if it is text.
- println: the function name
- ( ): parentheses hold the message
- "message": text to print, must be in quotes
kotlin
println("Your message here")
Example
This example shows how to print a simple greeting and a number using println(). Each call prints on a new line.
kotlin
fun main() {
println("Hello, Kotlin!")
println(123)
}Output
Hello, Kotlin!
123
Common Pitfalls
Common mistakes include forgetting quotes around text, which causes errors, or using print() when you want a new line after the message.
kotlin
// Wrong: missing quotes around text // println(Hello) // Right: text inside quotes println("Hello") // print() does not add a new line print("Hello") print(" World") // println() adds a new line println("Hello") println("World")
Output
Hello World
Hello
World
Quick Reference
| Function | Description |
|---|---|
| println(message) | Prints message and moves to next line |
| print(message) | Prints message without moving to next line |
| println() | Prints a blank line |
Key Takeaways
Use println() to print messages with a new line in Kotlin.
Put text messages inside quotes within println().
Use print() if you want to print without a new line.
Forgetting quotes around text causes errors.
println() is simple and useful for quick console output.