Kotlin scratch files: experimenting fast
Creating a whole project to check what listOf(1, 2, 3).sum() returns is like renting a lorry to
carry a bag of rice. IntelliJ has a lighter tool for exactly this — the scratch file — and
learning it now means you will actually experiment, which is how you learn a language quickly.
What a scratch file is
A scratch file is a Kotlin file that lives outside any project, that you can run instantly and
edit freely. It is meant for throwaway experiments: try a snippet, see the result, delete it or keep
it. Crucially, IntelliJ can show the result of each line right next to it, so you see what every
expression evaluates to without a single println.
Scratch files use the .kts extension — Kotlin script. The difference from a normal .kt
file is that a script runs top to bottom with no fun main() required; you just write statements
and they execute in order. That is perfect for experiments and, as you will see, exactly how Gradle
build files work later.
Creating and running one
In IntelliJ:
- File → New → Scratch File (or the shortcut your platform shows in that menu).
- Choose Kotlin from the list.
- Type some code and run it with the green arrow at the top, or the run shortcut.
Try this:
val numbers = listOf(3, 1, 4, 1, 5, 9, 2, 6)
val sum = numbers.sum()
val sorted = numbers.sorted()
val evens = numbers.filter { it % 2 == 0 }
println(sum)
println(sorted)
println(evens)
Run it and the output panel shows:
31
[1, 1, 2, 3, 4, 5, 6, 9]
[4, 2, 6]
With IntelliJ's inline results enabled, you also see each line's value printed to its right as you
go — no need to wrap everything in println to inspect it. That immediate feedback is the whole
point: you form a hypothesis ("does sorted() change the original or return a new list?"), run,
and know the answer in a second.
Why this matters for learning
The fastest way to learn a language is to ask it questions and read the answers, constantly. Not sure whether integer division truncates? Do not look it up — ask:
println(7 / 2) // 3 — integer division truncates
println(7.0 / 2) // 3.5 — one operand is a Double, so the result is Double
println(7 % 2) // 1 — remainder
Not sure what a string method does? Try it:
val s = " Namaste, Pune "
println(s.trim()) // "Namaste, Pune"
println(s.uppercase()) // " NAMASTE, PUNE "
println(s.trim().split(", ")) // ["Namaste", "Pune"]
Each of these is a question answered in seconds. A developer who reaches for a scratch file whenever they are unsure learns faster than one who guesses — and they carry that habit into real work, where "let me just check" beats "I think it does…".
When to use each tool
You now have three ways to run Kotlin. Match the tool to the job:
| You want to… | Use |
|---|---|
| Check what one expression returns | Scratch file (inline results) |
| Build and run a real program or the capstone | A project with fun main() |
| Check something with nothing installed | The Kotlin Playground in a browser |
A scratch file is not where you build your program. It has no project structure, no tests, no dependency management — it is a workbench for experiments. Keep real code in a project; use scratch files to figure things out along the way.
The command-line equivalent
If you prefer the terminal, kotlinc runs a script directly, no jar needed:
kotlinc -script experiment.kts
This is handy on a machine without IntelliJ, or in a quick shell session. It is slower to start
than the IDE's scratch runner (the compiler spins up each time), but it is the same idea: run a
.kts top to bottom and see the output.
A habit worth forming now
For the rest of this course, whenever a lesson states a fact about Kotlin — "val cannot be
reassigned", "readLine() can be null", "map returns a new list" — do not just believe it.
Open a scratch file and prove it. Type the code, run it, watch it behave (or watch the compiler
refuse). The practice sections deliberately ask you to break things and read the error, and a
scratch file is where that costs you five seconds instead of five minutes. Reading about a language
teaches you less than one afternoon of asking it questions.
Check your work
What a scratch file is for. Throwaway experiments, run instantly, outside any project.
The extension scratch files use, and what it means. .kts — Kotlin script, which runs top to
bottom with no fun main().
The feature that makes them ideal for learning. Inline results — each line's value shown beside
it, no println needed.
What 7 / 2 evaluates to, and why. 3 — integer division truncates; 7.0 / 2 is 3.5
because one operand is a Double.
Why a scratch file is not for building your program. No project structure, tests or dependency management — it is a workbench, not a home.
How to run a script from the command line. kotlinc -script file.kts.
The habit to form. When a lesson states a fact, prove it in a scratch file rather than believing it.
Practice
- Create a Kotlin scratch file, run the
numbersexample, and confirm the three outputs. - Enable inline results (if not already) and watch each line's value appear beside it.
- Answer three questions with a scratch file: does
sorted()change the original list? what is10 / 3? what does"hello".reversed()give? - Prove, in a scratch file, that
val x = 1cannot be reassigned — writex = 2below it and read the compiler's refusal. - Run one
.ktsfile from the command line withkotlinc -scriptand compare the speed with the IDE. - For every remaining lesson in this course, keep a scratch file open and test at least one claim the lesson makes. Start now, with one claim from this lesson.
Official documentation
- IntelliJ IDEA — Scratch files — Creating, running and configuring inline results.
- Kotlin — Command-line compiler (scripts) — Running
.ktsfiles withkotlinc -script. - Kotlin Playground — The zero-install browser alternative.
Next module — Language Basics: values, types and the syntax in every file you write.
Stuck on this lesson?
Being stuck is part of it — but being stuck alone for three days is not. Our internship programme pairs this curriculum with code review and one-to-one help from working developers, and it is free.
About the internship