Scala tutorial: Hello world

This tutorial helps you get started with Scala. It explains what you need to write your first Scala program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a Scala program before this is the place to start. We have created this article, so you can get started with Scala, and move on to our sms examples. If you are already familiar with Scala, you can jump directly to one of the following SMS projects.

Scala sms examples:

Scala send sms with the HTTP rest api (code sample)
Scala send multiple sms with the HTTP rest api (code sample)
Scala schedule sms with the HTTP rest api (code sample)
Scala receive sms with the HTTP rest api (code sample)
Scala delete sms with the HTTP rest api (code sample)
Download the latest Scala sms api library from Github

What is Scala

Scala is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is IntelliJ

IntelliJ is a tool to write a Scala program. IntelliJ allows you to type in text using the Scala language, and it makes it possible for you to tell the computer, to read the text and execute the instructions. We use the term "Run" to tell computer to execute the instructions.

What is a Scala Hello world program

The Scala hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Scala

To write your first program in Scala:

  1. Check prerequisites
  2. Setup Java JDK
  3. Download IntelliJ
  4. Install Scala
  5. Create new Scala sbt project in IntelliJ
  6. Name the project and set JDK
  7. Write Hello World program in Scala
  8. Run your Scala Hello World program
  9. Check the output

Prerequisites

Here is what you need to get started. To write your first computer program in Scala, you need a Windows computer, the IntelliJ IDE, the Scala plugin, and example code presented below.

  • Windows 10 computer
  • A Java JDK
  • IntelliJ IDE
  • Scala plugin
  • Ozeki Hello World example project

Download a Java JDK

You might want to use this link: https://adoptopenjdk.net/ to download a Java JDK.

Video 1 - Download Java JDK

Download Java JDK

To get started with coding in Scala, you must first download Java JDK. Do this by visiting the following link: https://adoptopenjdk.net/. Click on the blue Latest Release button to begin downloading the Java JDK installer (Figure 1). Once it has finished downloading, Java JDK is ready to install.

download the java jdk
Figure 1 - Download Java JDK

Install Java JDK

Video 2 - Install Java JDK

In order to understand the Scala language, our computer has to learn the language. To do that we have to install the Java JDK, that contains every information about it.

How to test if Java JDK is installed?

In two easy stepps you can test if the Java JDK is installed:

  1. Open the Command Prompt
  2. Type in the following command:

java -version
	

Code 1 - Command to test if Java is installed

If java is installed you will see something like this:

Test Java

Before starting out, you should make sure Java is properly installed. To do this, you first need to open the Command Prompt. Type in the following command: java -version and hit enter (Figure 2). If the Command Prompt returns the current version of Java installed, it is in working order.

Figure 2 - The version of the installed Java

Download IntelliJ

You might want to use this link: https://jetbrains.com/idea/download/ to download IntelliJ.

Video 3 - Download IntelliJ

Download IntelliJ

Now that Java JDK is installed, you can download an IDE for coding. Download IntelliJ from this link: https://jetbrains.com/idea/download/ Click on the download button under Community to begin downloading the installer. After the download has finished, you can install IntelliJ.

download intellij
Figure 3 - Download IntelliJ

Install IntelliJ

To write your first Scala program, you need to install IntelliJ. The following video shows you how this installation can be done.

Video 4 - Install IntelliJ

Install Scala

To create a Scala project in IntelliJ we have to install the Scala plugin. In the following video, I will show you how to install the Scala plugin in the IntelliJ IDE.

Video 5 - Install Scala

Install the Scala plugin

To start coding in Scala, you must first install the Scala plugin in IntelliJ. Upon starting the IDE, click on the plugins tab on the left. Type "Scala" into the search bar (Figure 4). The search results will return the Scala Plugin. Click on it, then click on Install to set up this plugin.

install the scala plugin in intellij
Figure 4 - Install the Scala plugin

Scala installed

After you have successfully installed the Scala plugin, it will appear in the Installed tab. When you click on it to view the details, it will read "Enabled for all projects".

installed scala plugin in intellij
Figure 5 - The installed Scala plugin

Create new project in IntelliJ

In order to start writing our first Scala program we have to create a new project in the IntelliJ IDE. In the following video you can see how to createa a new Scala project in IntelliJ.

Video 6 - How to create a new Scala project in IntelliJ

Create a new project

Everything is now set up to creat a new project and get started with coding in Scala. Click on the plus sign that reads New Project in the Projects tab (Figure 7). This will take you through several configuration settings, where you can set up the details of your project.

creating a project in intellij
Figure 7 - Create a new project

Select Scala

After creating a new project, you must configure it specifically for coding in Scala. First, select Scala from the menu on the left (Figure 8). Then, click on sbt. Finally, click Next.

new scala project in intellij
Figure 8 - Select Scala

Name your project and select Java JDK

On this screen you will be asked to name your project. Type your desired name into the textbox on the top (Figure 9). You may also have to select JDK, if it has not been already selected by default. Click on Finish to create your project.

naming your project in intellij
Figure 9 - Name your project, and select the Java JDK

Write hello world program in Scala

The first program you write in any programming language is the 'Hello world' program. The sole aim of this program is to print the term 'Hello world' to the computer screen. In this example the program consists of two lines of code: The third line: println("Hello world!") prints the text.

object helloWorld {
  def main(args:Array[String]) : Unit = {
    println("Hello world!)
  }
}	
	

Code 2 - The hello world program in Scala

Video 7 - Writing a hello world program in Scala

Run hello world program

In order to run your code for the first time in IntelliJ, you have to press F4 or

Video 8 - How to run your code in IntelliJ for the first time

Output of your code

When you run your code inside of IntelliJ, the output will appear in the debug console. This console is located in the bottom of the window. It contains the outputs and possible error messages your code returns. Here we can see that the "Hello world!" message appeared after the code ran (Figure 10). Now let's see what happens when Scala encounters an error.

hello world in scala
Figure 10 - The output of the code

What if I make a mistake in my code

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in Scala and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in Scala?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Video 9 - Making and fixing an error in IntelliJ

Error report in IntelliJ

IntelliJ notifies the developer about errors in multiple ways. First of all, the text editor underlines parts of the code that are going to lead to an error. It does this even before you run your code, so it is easy to identify basic typos (Figure 11). We decided to run the code anyway to demonstrate an error report. Error reports appear inside the debug console after the program failed to run. These reports are incredibly useful, because they indicate the location and type of error to the developer.

Figure 11 - Error report in IntelliJ

Summary

By reading this guide, you have attained the amount of knowledge to begin coding in Scala, to build and run your first Hello World program. If you have followed the instructions above and your program runs well, you have taken the initial steps to become a Scala software developer. Now you can use the Ozeki Scala SMS solutions which will help you to organize messages very effectively and effortlessly because you can customize the given codes without any limitations.

Learn more about developing your messaging system by using Scala solutions. Move on to the next tutorial about how to send SMS from Scala.

Let's put into practice what you have learned. Download the Ozeki SMS Gateway now!

More information