Your First Scala Program
Writing your first Scala program means choosing an entry point, producing output, and knowing how to turn source code into a running process. Scala 3 introduced the @main annotation as a lightweight way to mark a top-level function as the program's starting point, while println and print remain the standard ways to write to the console. Once written, a Scala source file can be turned into a running program either with the raw compiler tools (scalac and scala) or, inside a full project, with a single sbt run.
Cricket analogy: Writing your first Scala program is like batting your first delivery in international cricket — the @main annotation (or def main) is your guard taken at the crease, println is the shot you play that produces a visible, recorded outcome, and the choice between scalac/scala or sbt run is like choosing net practice versus a full official match setup.
Writing a Program with @main (Scala 3) or def main (Scala 2)
Scala 3's @main def greetUser(): Unit = { ... } and Scala 2's object GreetUserApp { def main(args: Array[String]): Unit = { ... } } both define valid program entry points and can both accept command-line arguments, but @main removes the boilerplate of wrapping the function in an object and declaring the args: Array[String] parameter explicitly — the compiler generates that wiring for you from the annotated function's own parameter list.
Cricket analogy: Scala 3's @main def greet(name: String) = println(s"Hello, $name") versus Scala 2's object Greet { def main(args: Array[String]) = ... } is like the difference between a modern DRS review system that directly flags the decision versus the older method requiring the third umpire to manually replay footage — both reach the same result, but @main removes boilerplate.
Compiling and Running: scalac, scala, and sbt run
To go from source code to a running program without sbt, you run scalac GreetUser.scala to compile the file into JVM bytecode, then scala GreetUserApp (or whatever class was generated) to execute it. Inside a full sbt project, both steps collapse into the single sbt run command, and for quick single-file experiments, the modern scala-cli tool can compile and run a .scala file directly with no project scaffolding at all.
Cricket analogy: Compiling with scalac then running with scala is like a net bowler warming up separately before stepping onto the main square, while sbt run bundles compiling and running into one integrated sequence, similar to a fully managed IPL match-day routine handling warm-up, toss, and play as one coordinated process.
Reading Input and Basic Program Structure
Interactive programs commonly need to read input rather than just print output: scala.io.StdIn.readLine() reads a single line typed by the user and, importantly, blocks the program's execution at that exact line until Enter is pressed, which is expected behavior rather than the program hanging or erroring. Combined with string interpolation (s"Hello, $name"), reading input and printing output are the two building blocks needed to write a simple but genuinely interactive console program.
Cricket analogy: Using scala.io.StdIn.readLine() to capture a player's name before printing a greeting is like a stadium announcer waiting for the toss result to be confirmed before reading out the starting line-ups — the program pauses execution until it receives that input, just as the announcement waits on the coin flip.
// Scala 3 style entry point using @main
import scala.io.StdIn.readLine
@main def greetUser(): Unit = {
print("What's your name? ")
val name = readLine()
println(s"Hello, $name! Welcome to Scala.")
}
// Equivalent Scala 2 style entry point
object GreetUserApp {
def main(args: Array[String]): Unit = {
print("What's your name? ")
val name = scala.io.StdIn.readLine()
println(s"Hello, $name! Welcome to Scala.")
}
}
// Compile and run from the terminal (Scala 3):
// $ scalac GreetUser.scala
// $ scala GreetUserApp
//
// Or, inside an sbt project:
// $ sbt run
For quick experiments or single-file scripts, scala-cli (the modern Scala command-line tool) lets you run a .scala file directly — scala-cli run GreetUser.scala — without setting up a full sbt project, downloading dependencies inline via special //> using directives at the top of the file.
In Scala 3, avoid the old object Main extends App pattern for new code — it relies on the deprecated DelayedInit mechanism, which can cause surprising behavior with static initializers and doesn't work well with certain JVM optimizations. Prefer the @main annotation on a top-level def, or an explicit def main(args: Array[String]): Unit method, both of which have straightforward, predictable semantics.
- Scala 3 programs can define an entry point with the @main annotation on a top-level function.
- Scala 2 programs traditionally define entry points with def main(args: Array[String]): Unit inside an object.
- println and print write to standard output; the interpolated string syntax s"..." is commonly used to build output messages.
- scalac compiles source files to .class files; scala runs a compiled program on the JVM.
- sbt run combines compiling and running into a single command inside a managed project.
- scala.io.StdIn.readLine() blocks execution until the user provides console input.
- scala-cli offers a lightweight way to run single .scala files without a full sbt project.
Practice what you learned
1. What is the Scala 3 preferred way to declare a program's entry point?
2. What does scala.io.StdIn.readLine() do?
3. What command combines compiling and running inside an sbt project?
4. Why is object Main extends App discouraged in modern Scala?
5. What tool allows running a single .scala file without setting up a full sbt project?
Was this page helpful?
You May Also Like
What Is Scala?
An introduction to Scala as a statically typed, JVM-based language that fuses object-oriented and functional programming paradigms.
Setting Up Scala and sbt
A practical walkthrough of installing Scala, understanding sbt (the Scala Build Tool), and creating your first buildable project.
vals, vars, and Types
How Scala's val/var distinction enforces immutability by default, and how the static type system infers and checks types at compile time.
Scala Operators and Expressions
How Scala treats operators as method calls, covers arithmetic, comparison, and logical operators, and why almost everything in Scala is an expression that returns a value.
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