Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Scala Tutorial - First Steps in scala - Testingpool.com

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

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).

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.

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.

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.

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.

abstractcasecatchclass
defdoelseextends
falsefinalfinallyfor
forSomeifimplicitimport
lazymatchnewNull
objectoverridepackageprivate
protectedreturnsealedsuper
thisthrowtraitTry
truetypevalVar
whilewithyield 
:==>
<-<:<%>:
#@
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...