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
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 nullExplanation
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
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
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
1. What is the return type of Kotlin's readLine() function?
2. What is the difference between println() and print() in Kotlin?
3. Which expression safely provides a default value of "Guest" if readLine() returns null?
4. What happens if you call readLine()!! and the input is null?
5. Which function safely converts a String to an Int, returning null instead of throwing on invalid input?
Was this page helpful?
You May Also Like
Type Conversion in Kotlin
Understand why Kotlin requires explicit conversion between numeric types and how to use functions like toInt() and toDouble().
String Templates in Kotlin
Learn how Kotlin's built-in string interpolation with $variable and ${expression} simplifies building text.
Null Safety in Kotlin
Kotlin's type system distinguishes nullable from non-nullable types at compile time, eliminating most NullPointerExceptions before code ever runs.
Safe Calls and the Elvis Operator in Kotlin
The ?. safe call and ?: Elvis operator let you access members of nullable values and supply defaults without verbose null checks.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics