Beginner

Hello World in Scala

Your first Scala program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Scala program and explore the elegance of this JVM language.

The Code

Create a file named HelloWorld.scala:

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

Understanding the Code

  • object HelloWorld - Defines a singleton object, Scala’s way of creating static members and entry points.
  • def main(args: Array[String]): Unit - The entry point method. Unit is Scala’s equivalent to Java’s void.
  • println() - A built-in function that prints to the console with a newline.

Scala’s Object-Oriented Twist

Unlike Java, Scala uses object instead of static. An object is a singleton instance—there’s exactly one HelloWorld in memory. This is more elegant than Java’s class-with-static-methods approach.

Running with Docker

The easiest way to run this without installing Scala locally:

1
2
3
4
5
# Pull the Scala Docker image
docker pull williamyeh/scala:latest

# Run the program (Scala scripts don't require separate compilation)
docker run --rm -v $(pwd):/app -w /app williamyeh/scala:latest scala HelloWorld.scala

Running Locally

If you have Scala installed:

1
2
3
4
5
6
# Run directly (interpreted mode)
scala HelloWorld.scala

# Or compile and run
scalac HelloWorld.scala
scala HelloWorld

Expected Output

Hello, World!

Alternative: Even More Concise

Scala also supports a top-level script style (Scala 3):

1
2
3
@main def hello(): Unit = {
  println("Hello, World!")
}

Or using the new Scala 3 syntax without braces:

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

These modern approaches use the @main annotation to designate the entry point, eliminating the need for an object wrapper.

Key Concepts

  1. JVM Language - Scala compiles to Java bytecode and runs on the JVM
  2. Object vs Class - Scala uses singleton objects for static-like functionality
  3. Type Inference - While we specified types here, Scala can often infer them
  4. Concise Syntax - Scala reduces boilerplate compared to Java
  5. Script Mode - Scala files can be run directly without explicit compilation

Scala vs Java

Compare this Scala version to the Java equivalent:

Java:

1
2
3
4
5
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Scala:

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

Notice the differences:

  • No public keyword needed (public by default)
  • object instead of class with static
  • def for method definitions
  • No semicolons required
  • println is a top-level function, not System.out.println

What Makes Scala Special?

Even in this simple example, we see Scala’s philosophy:

  • Concise but clear: Less ceremony than Java
  • Hybrid paradigm: Object-oriented (singleton object) meets functional (first-class functions)
  • JVM compatibility: Full access to Java libraries
  • Modern features: Script mode, type inference, and optional syntax

Next Steps

Continue to explore Scala’s powerful features:

  • Variables and immutability (val vs var)
  • Collections and functional operations
  • Pattern matching
  • Higher-order functions
  • Type inference and type safety

Scala scales from simple scripts like this to massive distributed systems, making it a truly “scalable language.”

Running Today

All examples can be run using Docker:

docker pull williamyeh/scala:latest
Last updated: