Scala Hello World Program

Welcome to Scala! In this tutorial, we'll guide you through creating your first Scala program—a classic "Hello, World!" application.

Writing the Program

link to this section

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

  1. Open a Text Editor : Open your favorite text editor or integrated development environment (IDE) to write your Scala code.

  2. Create a New Scala File : Create a new file with a .scala extension—for example, HelloWorld.scala .

  3. Write Your Scala Code : In your Scala file, write the following code:

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

This Scala code defines an object named HelloWorld with a main method—the entry point of the program. Inside the main method, we use the println function to print "Hello, World!" to the console.

Let's break down the code in more detail:

  • object HelloWorld : In Scala, the object keyword is used to define a singleton object. Here, we're defining an object named HelloWorld .

  • def main(args: Array[String]): Unit = { } : Within the HelloWorld object, we define a main method. This method is the entry point of the program. It takes an array of strings ( args ) as input and returns Unit , which is similar to void in other languages. The main method is where the execution of the program begins.

  • println("Hello, World!") : Inside the main method, we use the println function to print "Hello, World!" to the console. println is a standard Scala function used to print text to the standard output.

  • Understanding args: Array[String] : The args parameter represents command-line arguments passed to the program. It's an array of strings ( Array[String] ). In this simple "Hello, World!" program, we don't use the command-line arguments, but in more complex programs, you can access and process these arguments within the main method.

  • Unit Return Type : The main method has a return type of Unit , which means it doesn't return any value. In Scala, methods that don't return anything explicitly have a return type of Unit .

Compiling and Running Your Program

link to this section

To compile and run your Scala program, follow these steps:

  1. Open a Terminal or Command Prompt : Navigate to the directory where you saved your Scala file using the terminal or command prompt.

  2. Compile Your Scala Program : Use the scalac command to compile your Scala file. For example:

scalac HelloWorld.scala 
  1. Run Your Scala Program : After successfully compiling your Scala file, you'll see a .class file generated in the same directory. Now, use the scala command to execute your program:
scala HelloWorld 

You should see the output:

Hello, World! 

Congratulations! You've successfully created and executed your first Scala program.

Conclusion

link to this section

In this tutorial, we focused on creating a simple "Hello, World!" program in Scala. Now that you've learned the basics, you're ready to explore more of Scala's powerful features and build exciting applications. Stay tuned for more Scala tutorials!