Your First Scala Program: Hello, World!

Introduction

Every programming journey begins with "Hello, World!" Let's write your first Scala program and understand its structure.

The Traditional Approach

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, World!")
  }
}

Using the App Trait

Scala provides a more concise way:

object HelloWorld extends App {
  println("Hello, World!")
}

Project Structure

A typical Scala project looks like this:

my-project/
├── build.sbt
├── project/
│   └── build.properties
└── src/
    └── main/
        └── scala/
            └── HelloWorld.scala

Running Your Program

# Compile and run with sbt
sbt run

# Or compile to bytecode
sbt compile

Understanding the Code

  • object creates a singleton
  • extends App provides a main method
  • println outputs to console
  • No semicolons required!

Summary

You've written your first Scala program! The syntax is clean and expressive.

What's Next

Next, we'll explore variables and immutability with val, var, and lazy val.