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 running 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 simplest program you can write. It simply prints out the sentence: 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:
- Check prerequisites
- Setup Java JDK
- Download IntelliJ
- Install Scala
- Create new Scala sbt project in IntelliJ
- Name the project and set JDK
- Write Hello World program in Scala
- Run your Scala Hello World program
- 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.
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.
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 steps you can test if the Java JDK is installed:
- Open the Command Prompt
- Type in the following command:
java -version
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.
Download IntelliJ
You might want to use this link: https://jetbrains.com/idea/download/ to 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. (Figure 3)
Install IntelliJ
To write your first Scala program, you need to install IntelliJ. The following video shows you how this installation can be done.
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.
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.
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". (Figure 5)
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 create a new Scala project in IntelliJ.
Create a new project
Everything is now set up to create a new project and get started with coding in Scala. Click on the plus sign that reads New Project in the Projects tab (Figure 6). This will take you through several configuration settings, where you can set up the details of your 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 7). Then, click on sbt. Finally, click Next.
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 8). You may also have to select JDK, if it has not been already selected by default. Click on Finish to create your project.
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!) } }
Run hello world program
In order to run your code for the first time in IntelliJ, you have to press F4 or
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 9). Now let's see what happens when Scala encounters an error.
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.
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 10). 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.
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
- C# SMS tutorial: Hello world
- Visual Basic SMS tutorial: Hello world
- F# SMS tutorial: Hello world
- Java SMS tutorial: Hello world
- Javascript SMS tutorial: Hello world
- Node.js SMS tutorial: Hello world
- Kotlin SMS tutorial: Hello world
- C/C SMS tutorial: Hello world
- Delphi SMS tutorial: Hello world
- PHP SMS tutorial: Hello world
- Perl SMS tutorial: Hello world
- Python SMS tutorial: Hello world
- Python flask SMS tutorial: Hello world
- Ruby SMS tutorial: Hello world
- Tcl/Tk SMS tutorial: Hello world
- Go SMS tutorial: Hello world
- R SMS tutorial: Hello world
- Scala SMS tutorial: Hello world
- Objective C SMS tutorial: Hello world
- Tutorials for software developers