Saturday, September 27, 2014

Introduction to Scala

Scala is an acronym for Scalable Language. Scala integrates the features of both object oriented and functional languages. It's one of the languages which runs on JVM. The Scala is designed to be concise, elegant and type-safe.

Features

To keep the discussion short, i will not detail each and every feature but list some of them.
  • Object oriented
  • Functional Language
  • Statically Typed
  • Runs on JVM
  • Can execute Java Code

Getting Started

Like JDK, Scala comes with
  • scalac - Scala compiler which compiles the Scala code to byte code like javac command
  • scala - Scala interpreter and command line (called as REPL) to execute the byte code like java command
When you launch scala (without any arguments) prints the scala version and opens the scala command line, where you can execute the scala commands as below

Scala Interpretor

The interpreter helps us to be familiar with the scala language features like expressions, variables, types etc. before writing the Scala programs. The Scala Interpreter is called REPL - Read Evaluate Print Loop.


Variables and Assignments


Variables are created using var keyword and assign values with assignment operator (=).

Constants


Constants can be defined using val. Unlike variables these can't be re-assigned. These are like final keyword in Java. See below

Functions


Functions are the most important in any programming language to avoid the code duplication and organizing the code into blocks for more readability. The functions are defined in Scala using def keyword

Program Files and Running using scala command

Commands in a file


Instead of using interpreter to run the commands, write into a file with an extension .scala and call using the scala command.
The following commands are added to a file called Commands.scala
println("Hello World")
val x=20
val y=30
var z=x+y
println(z)
Run the file using scala command
# scala Commands.scala 
Hello World
50
#

Hello World using a Class


The below class is written into a file HelloWorld.scala.

object HelloWorld {
   def main(args:Array[String]) {
       if(args.length < 1)
         println("Enter your name!!!!");
       else
         println("Hello "+args(0)+"!!");
   }
}

Points to be noted 

  • HelloWorld is a class which is defined with object keyword (like class keyword in java)
  • main the method where the program start which has array of strings as an argument
  • def is the keyword to be used to define a method (rather function) 
  • The syntax is almost similar to Java language as you see in if and else conditional statement.
  • Array elements are accessed using ( unlike in Java [

Run the program

Run the program using scala command 
# scala HelloWorld.scala     
Enter your name!!!!
# scala HelloWorld.scala Veeru
Hello Veeru!!

Compile the Program


Instead, you can compile the scala program into a byte code and then run using scala command. To compile the program use scalac command.  

# scalac HelloWorld.scala 

This will create a class file called HelloWorld.class which is byte code created for the class HelloWorld.

Run the Program

Run the program using the command scala.
# scala HelloWorld      
Enter your name!!!!
# scala HelloWorld Veeru
Hello Veeru!!

As simple as that. If you have an idea of how to create and execute a Java program, Scala is almost same except the compiler and interpreter commands.


Happy Learning!!!!