Scala Hello World

Whichever programming language you are comfortable with, you must be familiar with a Hello World program in it. This program is generally the first program that you will ever create in any programming language. With this basic program, you try to understand the syntax of the programming language that you wish to learn. In this guide, we will learn to write a Hello World program in the Scala programming language in Ubuntu 20.04.

The Hello World Script in Scala Using the Text Editor

A simple way to implement a basic Hello World script in the Scala programming language is by using any text editor present on your system. You first need to create a file with any name of your choice with the “.Scala” extension within the Home directory of your system. We have named our file “HelloWorld.Scala”. After opening this file, we wrote the script shown in the image below:

Now, we will try to understand all the components of this Scala script one by one. First, we have created a Scala class named “HelloWorld” with the “object” keyword. This will act as a driver class for our Scala script. Then, we have created the “main()” function for our Scala script. The “args: Array[String]” notation refers to the arguments of this function. Basically, the “main()” function is also capable of accepting command line arguments. Now, within this function, we wanted to insert a comment for easier understanding.

The single liner comments in Scala can be inserted with double forward slashes “//”. Then, we just wanted to print a Hello World message on the terminal for which we have used the “println” statement. This, in fact, serves as the actual functionality of our current Scala script. Then, we have just saved our Scala script.

How to Compile a Scala Script?

A Scala script can easily be compiled by using the following command:

$ scalac HelloWorld.Scala

Here, HelloWorld.Scala represents the name of our Scala script file. Running this command will create a class file for our Scala script with the name HelloWorld.class that can conveniently be executed.

How to Execute a Scala Script?

You can execute a Scala script with the command shown below:

$ scala HelloWorld

Here, HelloWorld represents the name of our driver class within our Scala script. The class file created in the step shown above can only be executed with the help of this command.

Upon execution, the Hello World message appeared on the terminal as exhibited in the following image. An important point to be noted over here is that the comments are only there for easier understanding and readability of the code. It means that the comments will not appear in the output of your program as you can witness from the output of our Hello World script.

The Hello World Scala Script with Command Line Arguments

We can make our Hello World program a little complex by introducing the command line arguments to it. For that, you can write a Scala script similar to the one shown in the image below:

In this script, we have used multi liner comments in Scala which can be inserted by enclosing them in between the “/* ….. */” symbols. After that, we have printed a Hello World message using the “println” statement. Then, we have used a “for” loop that iterates through all the arguments that will be passed to this Scala script at runtime. Finally, we have used the “println” statement for printing all the command line arguments on the terminal in separate lines.

This Scala script can be compiled in the same way as we have explained above, however, its execution will differ slightly. To execute this Scala script, we have used the following command:

$ scala HelloWorld One Two Three

Here, HelloWorld is simply the name of our driver class, however, “One”, “Two”, and “Three” are the command line arguments that we wanted to pass on to our Scala script. You can pass any arguments of your choice to this Scala script. Moreover, the number of arguments can also vary depending upon your requirements.

Upon execution, this Scala script exhibited the output revealed in the image below. You can see that along with displaying the Hello World message, this script also printed the command line arguments on the terminal.

Scala Hello World using Scala REPL

A yet another simple way for printing a Hello World message in Scala is by using Scala REPL which is a command line interpreter shell for Scala. All you need to do is to gain access to the REPL shell and then you will easily be able to print a Hello World message in it. For entering the Scala REPL, you need to execute the subsequent command in your system’s terminal:

$ scala

The Scala REPL shell looks something like shown in the image below:

While staying within this shell, you just need to run the following command:

> println(“Hello World!);

As soon as you will hit the Enter key after typing this command, the Hello World message will appear on your Scala REPL shell as shown in the image below. This is how you can easily print such a message without even having the need of creating a separate Scala script for it.


In this guide, we wanted to discuss the purpose of the most basic program of any programming language i.e., the Hello World program especially, with respect to the Scala programming language. With the help of this program, we also wanted to talk about certain important components of a basic Scala script. We also shared with you how a Scala script can be compiled and executed. In short, we shared with you the two main methods of printing a Hello World message in Scala i.e., the script-based method and the Scala REPL method. By using either of these methods, you can instantly print a Hello World message in Scala whenever you need to.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.