100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Input and Output in Kotlin

Learn how to print output with println()/print() and read user input with readLine() in Kotlin.

BasicsBeginner7 min readJul 8, 2026
Analogies

Introduction

Every interactive program needs a way to display information and accept input. Kotlin provides println() and print() for writing to standard output, and readLine() for reading a line of text from standard input. Because input from a console is not guaranteed to exist (for example if input is piped from an empty source), readLine() returns a nullable String? type, which fits naturally into Kotlin's null-safety system.

🏏

Cricket analogy: Like a scoreboard operator who always posts the score (println always writes) but a rain-delay radio update that might return 'no signal' (readLine() returning null), Kotlin types that possible absence as String? so you must handle it.

Syntax

kotlin
println("Hello, World!")   // prints with a newline
print("No newline here ")  // prints without a newline

val line: String? = readLine()   // nullable result
val safeLine: String = readLine() ?: "default"
val forced: String = readLine()!!  // throws if null

Explanation

println() writes its argument followed by a newline character, while print() writes without adding a newline. Both accept any type, converting it to a String using toString() automatically, and both support string templates for building formatted output. readLine() reads a full line of text typed by the user and returns String?, since there might be no more input (e.g. end of stream). You typically handle the nullable result with the Elvis operator (?:) to provide a default, or with the not-null assertion operator (!!) if you are certain input will be present and want to fail fast otherwise. When reading numbers, combine readLine() with a conversion function such as .toIntOrNull() to safely parse text into a number without crashing on invalid input.

🏏

Cricket analogy: Like an over-by-over commentary line that always starts fresh (println with newline) versus a continuous ball-by-ball ticker (print without), a nullable run-rate reading is handled with Elvis (?: 0.0) or force-unwrapped with !!, and toIntOrNull() safely parses a scoreboard entry without crashing on a smudged digit.

Example

kotlin
fun main() {
    print("Enter your name: ")
    val name = readLine() ?: "Guest"

    print("Enter your age: ")
    val ageInput = readLine()
    val age = ageInput?.toIntOrNull() ?: 0

    println("Hello, $name! In 5 years you'll be ${age + 5}.")
}

Output

text
Enter your name: Sam
Enter your age: 30
Hello, Sam! In 5 years you'll be 35.

Key Takeaways

  • println() outputs text followed by a newline; print() outputs without a newline.
  • readLine() reads a line from standard input and returns a nullable String?.
  • Use the Elvis operator (?:) to supply a default when readLine() returns null.
  • The not-null assertion operator (!!) forces a non-null result but throws an exception if the value is null.
  • Combine readLine() with toIntOrNull() or similar functions to safely parse numeric input.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#InputAndOutputInKotlin#Input#Output#Syntax#Explanation#StudyNotes#SkillVeris