Scala – First Steps

In this tutorial, you will get familiar with basic syntax of scala which will help you to understand detailed chapters on scala.

If you are coming from Java background, it will be easy for you to learn Scala.The biggest syntactic difference between Scala and Java is that the ‘;’ character is optional to end the line. You need scala to be installed on your set up, if it is not done yet – Click here. We can execute a Scala program in two modes: one is scala interpreter and another is script mode.

Learn with Scala Interpreter:

The easiest way to get started with Scala is by using the Scala interpreter, an interactive “shell” for writing Scala expressions and programs. You just need to type an expression into interpreter and it will evaluate the expression and print the result. To use the scala interpreter, simply type ‘Scala’.

Scala Interpreter

To see how it works, simply type 1+2 and hit enter. Interpreter will print the result which is 3.

Scala Tutorial

In the above output, res0 is auto-generated name which means result 0 and it can be further used as a variable. res0 * 3 will be 9.

Scala Tutorial

To print something, we use the println() function. The println function prints the passed string to the standard output, similar to System.out.println in Java.

Scala Tutorial

Learn how to define variables:

Scala has two kind of variables – vals and vars. Once a val variable is assigned, it can not be reassigned – similar to java’s final variable. As you can see below, we have assigned a value to the variable msg which is a val. But when we tried to reassign the value to msg, it has thrown the error.

Scala Tutorial

But reassignment is possible if any variable is defined as var.

Scala Tutorial

Learn with Script Mode:

You can also run scala program like a script. For that, open a notepad and type the below program and save it as FirstProgram.scala

In the above code, we have create an object named ‘FirstProgram’ having main method which prints a message ‘My First Program’. First go the directory in the command prompt where the file is stored.

To compile the code, use command – scalac FirstProgram.scala ( It will create a file FirstProgram.class).

To execute the compiled code, use command – scala FirstProgram (It will print the result).

Scala Tutorial

You can also use IDE (Integrated Development Environment) for executing scala code.

The above example is written using object oriented approach. You can also use functional approach to write code in scala as shown below.

To enter something into interpreter that is having multiple lines, just keep typing after the first line. If code you are typing is not completed so far, the interpreter will respond with the vertical bar on the next line.

Scala Tutorial

f you realize you have typed something wrong, but the interpreter is still waiting for more input, you can escape by pressing enter twice.

Scala Tutorial

Comments in Scala

Scala supports single-line and multi-line comments very similar to Java. For multi-line comments, use /*—- */ and for single line comment use double slash.

Scala Keywords

The following list shows the reserved words in Scala. These reserved words may not be used as constant or variable or any other identifier names.

abstractcasecatchclass
defdoelseextends
falsefinalfinallyfor
forSomeifimplicitimport
lazymatchnewNull
objectoverridepackageprivate
protectedreturnsealedsuper
thisthrowtraitTry
truetypevalVar
whilewithyield 
:==>
<-<:<%>:
#@
Print Friendly, PDF & Email
Scala Overview
Scala - Data Types
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *