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’.
To see how it works, simply type 1+2 and hit enter. Interpreter will print the result which is 3.
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.
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.
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.
But reassignment is possible if any variable is defined as var.
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
object FirstProgram { /* This is my first scala program. * This will print 'My First Program' as the output */ def main(args: Array[String]) { println("My First Program") // prints 'My First Program' } }
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).
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.
def FirstProgram{
println("My First Program")
}
FirstProgram // Calling of function
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.
f you realize you have typed something wrong, but the interpreter is still waiting for more input, you can escape by pressing enter twice.
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.
object FirstProgram {
/* This is my first scala program.
* This will print 'My First Program' as the output - Multi-line comment
*/
def main(args: Array[String]) {
println("My First Program") // prints 'My First Program' - Single line comment
}
}
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.
abstract | case | catch | class |
def | do | else | extends |
false | final | finally | for |
forSome | if | implicit | import |
lazy | match | new | Null |
object | override | package | private |
protected | return | sealed | super |
this | throw | trait | Try |
true | type | val | Var |
while | with | yield | |
– | : | = | => |
<- | <: | <% | >: |
# | @ |