Writing Your First Scala Program: The Essential "Hello, World!" Tutorial

Introduction

link to this section

Embarking on your journey with Scala? A traditional way to get acquainted with a new programming language is by writing a simple "Hello, World!" program. This blog post will guide you through creating, compiling, and running your first Scala program, providing valuable insights into Scala's basic structure and syntax.

Table of Contents:

  1. The Structure of a Scala Program

  2. Creating the "Hello, World!" Program

  3. Compiling and Running the Program

    1. Using the Scala Compiler
    2. Using a Build Tool
      1. sbt
      2. Maven
      3. Gradle
  4. Understanding the "Hello, World!" Program

  5. Exploring Variations of the "Hello, World!" Program

  6. Conclusion

The Structure of a Scala Program

link to this section

Before diving into the "Hello, World!" program, let's briefly examine the basic structure of a Scala program:

  • Scala programs typically consist of one or more source files containing classes, objects, and traits.
  • The entry point of a Scala program is the main method defined within an object, not a class.
  • Scala uses the .scala file extension for source files.

Creating the "Hello, World!" Program

link to this section

To create your first "Hello, World!" program in Scala, follow these steps:

  1. Open your preferred text editor or Integrated Development Environment (IDE).
  2. Create a new file and name it HelloWorld.scala .
  3. Copy and paste the following code into the file:
object HelloWorld { 
    def main(args: Array[String]): Unit = { 
        println("Hello, World!") 
    } 
} 
  1. Save the file.

Compiling and Running the Program

link to this section

There are several ways to compile and run your Scala program. In this tutorial, we will cover using the Scala compiler directly and using a build tool.

3.1 Using the Scala Compiler

To compile and run your "Hello, World!" program using the Scala compiler, follow these steps:

  1. Open a command prompt or terminal.
  2. Navigate to the directory containing your HelloWorld.scala file.
  3. Compile the program using the following command:
scalac HelloWorld.scala 

This command generates a HelloWorld.class file, which contains the bytecode for your program.

Run the compiled program using the following command:

link to this section
scala HelloWorld 

You should see the output "Hello, World!" in your command prompt or terminal.

3.2 Using a Build Tool

Alternatively, you can use a build tool like sbt, Maven, or Gradle to compile and run your Scala program. Each tool has its specific instructions, so refer to the documentation of your chosen build tool for guidance.

3.2.1 sbt

To compile and run your "Hello, World!" program using sbt, follow these steps:

  1. Create a new directory for your project.
  2. Inside the project directory, create a src/main/scala directory.
  3. Move your HelloWorld.scala file to the src/main/scala directory.
  4. Create a file named build.sbt in the project directory with the following content:
name := "HelloWorld" 
version := "1.0" 
scalaVersion := "2.13.8" 
  1. Open a command prompt or terminal, navigate to the project directory, and execute the following command to compile and run your program:
sbt run 

You should see the output "Hello, World!" in your command prompt or terminal

Understanding the "Hello, World!" Program

link to this section

Now that you've successfully compiled and run your "Hello, World!" program, let's break down the code to better understand its components:

object HelloWorld { 
    def main(args: Array[String]): Unit = { 
        println("Hello, World!") 
    } 
} 
  • object HelloWorld : This declares a singleton object named HelloWorld . In Scala, the entry point of a program is an object with a main method, not a class.
  • def main(args: Array[String]): Unit = { ... } : This defines the main method, which is the entry point of the program. The main method takes an array of strings (command-line arguments) and returns Unit (similar to void in Java).
  • println("Hello, World!") : This line prints "Hello, World!" to the console. println is a predefined method in Scala that writes a line to the standard output, followed by a newline.

Exploring Variations of the "Hello, World!" Program

link to this section

Scala is a versatile language that allows for different ways to achieve the same goal. Here's an alternative way to write the "Hello, World!" program using Scala's App trait:

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

In this version, the HelloWorld object extends the App trait, which provides a default implementation of the main method. This allows you to place your code directly inside the object body, and the App trait will take care of executing it when the program runs.

  1. Conclusion

Congratulations! You've written, compiled, and run your first Scala program. This "Hello, World!" program is a simple yet effective introduction to Scala's basic structure and syntax. As you continue learning and exploring Scala, you'll uncover the language's powerful features and flexibility, allowing you to create more sophisticated and expressive programs. Happy coding!