Scala First Program

Writing your first program in Scala is simpler than most languages. Scala 3 removes a lot of boilerplate that older versions required. You will write a program, understand each line, compile it, and run it — all in this topic.

The Classic Hello World

Open the Main.scala file inside your SBT project's src/main/scala/ folder. Replace the contents with:

@main def hello(): Unit =
  println("Hello, World!")

Run it from your terminal inside the project folder:

sbt run

Output:

Hello, World!

Breaking Down Every Part


@main def hello(): Unit =
  println("Hello, World!")

│
├── @main       → marks this as the program entry point
├── def         → keyword to define a function
├── hello       → the function name (you choose this)
├── ()          → empty parameter list (no inputs needed)
├── Unit        → return type; means "returns nothing"
├── =           → starts the function body
└── println(..) → built-in function that prints text + newline

The @main Annotation

In Scala 3, @main tells the compiler "this function is the starting point of the program." When you run the program, Scala looks for a function marked @main and executes it first. You can have only one @main function per application entry point.

Think of @main like the front door of a building. Every visitor enters through the front door. Every Scala program starts at the @main function.

println vs print

Scala provides two basic output functions:

println("Hello")   // prints text AND moves to a new line
print("Hello")     // prints text WITHOUT moving to a new line

Example showing the difference:

@main def showDifference(): Unit =
  print("One ")
  print("Two ")
  println("Three")
  println("Four")

Output:

One Two Three
Four

print kept "One" and "Two" on the same line. println("Three") added "Three" and then moved to the next line. "Four" started on a fresh line.

Printing Different Types of Data

You can print numbers, text, and calculated results directly:

@main def printVariety(): Unit =
  println(42)
  println(3.14)
  println(true)
  println("Scala is fun")
  println(10 + 5)

Output:

42
3.14
true
Scala is fun
15

Scala automatically converts numbers and booleans to their text representation when you pass them to println.

Adding Variables to Your Program

@main def greetUser(): Unit =
  val name = "Priya"
  val age = 28
  println("Name: " + name)
  println("Age: " + age)

Output:

Name: Priya
Age: 28

val creates a value that cannot change after you set it. The + operator joins pieces of text together — this is called string concatenation.

Program Structure Diagram


Main.scala file
┌─────────────────────────────────┐
│  @main def hello(): Unit =      │  ← Entry Point
│    val greeting = "Hello"       │  ← Store a value
│    val name     = "World"       │  ← Store another value
│    println(greeting + ", "      │  ← Join and print
│            + name + "!")        │
└─────────────────────────────────┘
         ↓ compile with SBT
┌─────────────────────────────────┐
│  JVM Bytecode (.class file)     │
└─────────────────────────────────┘
         ↓ run with SBT
┌─────────────────────────────────┐
│  Terminal Output: Hello, World! │
└─────────────────────────────────┘

Multiple Statements in One Program

A real program does more than print one line. You stack statements one below the other, and Scala runs them top to bottom:

@main def morningRoutine(): Unit =
  println("Wake up")
  println("Drink water")
  println("Write Scala code")
  println("Repeat tomorrow")

Output:

Wake up
Drink water
Write Scala code
Repeat tomorrow

Scala does not require semicolons at the end of each line. The newline itself marks the end of a statement. You can use semicolons if you want to put two statements on one line, but that style is uncommon.

Comments in Scala

Comments are notes in your code that the compiler ignores. Use them to explain what your code does:

@main def withComments(): Unit =
  // This is a single-line comment
  println("This line runs")

  /* This is a
     multi-line comment.
     The compiler skips all of this. */
  println("This line also runs")

Running Without SBT

For quick experiments, you can also compile and run a single file directly from the terminal without SBT:

scalac Main.scala    // compile
scala hello          // run (use the function name, not the file name)

SBT is better for real projects because it handles dependencies and multiple files automatically. The scalac/scala approach works well for learning and quick tests.

Common Beginner Errors

Missing Indentation in Scala 3

Scala 3 uses indentation to mark code blocks — similar to Python. The body of a function must be indented by at least one space or tab relative to the function header:

// WRONG
@main def bad(): Unit =
println("Not indented")   // compiler error

// CORRECT
@main def good(): Unit =
  println("Properly indented")  // works fine

Mismatched Quotes

Every opening quote needs a closing quote on the same line:

// WRONG
println("Hello)       // missing closing quote

// CORRECT
println("Hello")      // both quotes present

Wrong Function Name in @main

The function name after def is just a label. The @main annotation is what makes it the entry point — not the name. You can call it anything you like.

Your First Multi-Function Program

A Scala file can contain multiple functions. Only the one marked @main runs automatically:

def greet(name: String): Unit =
  println("Hello, " + name + "!")

def farewell(name: String): Unit =
  println("Goodbye, " + name + "!")

@main def run(): Unit =
  greet("Arjun")
  farewell("Arjun")

Output:

Hello, Arjun!
Goodbye, Arjun!

The greet and farewell functions each accept a name parameter of type String. The run function calls both. This is the foundation of structured programming in Scala.

Leave a Comment

Your email address will not be published. Required fields are marked *