Friday, April 27, 2012

Scala - Environment Setup


Scala, short for Scalable Language, is a hybrid functional programming language. It was created by Martin Odersky and it was first released in 2003.
Scala smoothly integrates features of object-oriented and functional languages and Scala is compiled to run on the Java Virtual Machine. Many existing companies who depend on Java for business critical applications are turning to Scala to boost their development productivity, applications scalability and overall reliability.
Here are the important list of features which makes Scala a first choice of the application developers.

Scala is object-oriented:


Scala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits which will be explained in subsequent chapters.
Classes are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.

Scala is functional:


Scala is also a functional language in the sense that every function is a value and because every value is an object so ultimately every function is an object.
Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. These concepts will be explained in subsequent chapters.

Scala is statically typed:

Scala, unlike some of the other statically typed languages, does not expect you to provide redundant type information. You don't have to specify a type in most cases, and you certainly don't have to repeat it.

Scala runs on the JVM:


Scala is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM). This means that Scala and Java has a common runtime platform. You can easily move from Java to Scala.
The Scala compiler compiles your Scala code into Java Byte Code which can then be executed by the scala command. The scala command is similar to the java command, in that it executes your compiled Scala code.

Scala can Execute Java Code:

Scala enables you to use all the classes of the Java SDK's in Scala, and also your own, custom Java classes, or your favourite Java open source projects.

Scala vs Java:

Scala has a set of features which differ from Java. Some of these are:
  • All types are objects.
  • Type inference.
  • Nested Functions.
  • Functions are objects.
  • Domain specific language (DSL) support.
  • Traits.
  • Closures.
  • Concurrency support inspired by Erlang.

Scala - Environment Setup:

The Scala language can be installed on any Unix-like or Windows system. Before you start installing Scala on your machine, you must have Java 1.5 or greater installed on your computer.

Installing Scala on Windows:

Step (1): JAVA Setup:

First, you must set the JAVA_HOME environment variable and add the JDK's bin directory to your PATH variable. To verify if everything is fine, at command prompt, type java -version and press Enter. You should see something like the following:
C:\>java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02, mixed mode)

C:\>
Next, test to see that the Java compiler is installed. Type javac -version. You should see something like the following:
C:\>javac -version
javac 1.6.0_15

C:\>

Step (2): Scala Setup:

Next you can download Scala from http://www.scala-lang.org/downloads. At the time of writing this tutorial I downloaded scala-2.9.0.1-installer.jar and put it in C:/> directory. Make sure you have admin privilege to proceed. Now execute the following command at command prompt:
C:\>java -jar scala-2.9.0.1-installer.jar

C:\>
Above command will display an installation wizard which will guide you to install scala on you windows machine. During installation, it will ask for license agreement, simply accept it and further it will ask a path where scala will be installed. I selected default given path C:\Program Files\scala, you can select a suitable path as per your convenience. Finally, open a new command prompt and type scala -version and press Enter. You should see the following:
C:\>scala -version
Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL

C:\>
Congratulations, you have installed Scala on your Windows machine. Next section will teach you how to install scala on your Mac OS X and Unix/Linux machines.

Installing Scala on Mac OS X and Linux

Step (1): JAVA Setup:

Make sure you have got the Java JDK 1.5 or greater installed on your computer and set JAVA_HOME environment variable and add the JDK's bin directory to your PATH variable. To verify if everything is fine, at command prompt, type java -version and press Enter. You should see something like the following:
$java -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) Server VM (build 1.5.0_22-b03, mixed mode)

$
Next, test to see that the Java compiler is installed. Type javac -version. You should see something like the following:
$javac -version
javac 1.5.0_22
javac: no source files
Usage: javac <options> <source files>
................................................

$

Step (2): Scala Setup:

Next you can download Scala from http://www.scala-lang.org/downloads. At the time of writing this tutorial I downloaded scala-2.9.0.1-installer.jar and put it in /tmp directory. Make sure you have admin privilege to proceed. Now execute the following command at command prompt:
$java -jar scala-2.9.0.1-installer.jar
Welcome to the installation of scala 2.9.0.1!
The homepage is at: http://scala-lang.org/
press 1 to continue, 2 to quit, 3 to redisplay
1
................................................
[ Starting to unpack ]
[ Processing package: Software Package Installation (1/1) ]
[ Unpacking finished ]
[ Console installation done ]

$
During installation, it will ask for license agreement, to accept it type 1 and it will ask a path where scala will be installed. I entered /usr/local/share, you can select a suitable path as per your convenience. Finally, open a new command prompt and type scala -version and press Enter. You should see the following:
$scala -version
Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL

$
Congratulations, you have installed Scala on your Unix/Linux machine.

Scala - Basic Syntax

If you have good understanding on Java then it will be very easy for you to learn Scala. The biggest syntactic difference between Scala and Java is that the ; line end character is optional. When we consider a Scala program it can be defined as a collection of objects that communicate via invoking each others methods. Let us now briefly look into what do class, object, methods and instant variables mean.
  • Object - Objects have states and behaviors. Example: A dog has states-color, name, breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
  • Class - A class can be defined as a template/ blue print that describe the behaviors/states that object of its type support.
  • Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Fields - Each object has its unique set of instant variables which are called fields. An object's state is created by the values assigned to these fields.

First Scala Program:

Interactive Mode Programming:

Invoking the interpreter without passing a script file as a parameter brings up the following prompt:
C:\>scala
Welcome to Scala version 2.9.0.1
Type in expressions to have them evaluated.
Type :help for more information.

scala>
Type the following text to the right of the Scala prompt and press the Enter key:
scala> println("Hello, Scala!");
This will produce following result:
Hello, Scala!

Script Mode Programming :

Let us look at a simple code that would print the words Hello, World!.
object HelloWorld {
   /* This is my first java program.  
    * This will print 'Hello World' as the output
    */
   def main(args: Array[String]) {
      println("Hello, world!") // prints Hello World
   }
}
Let's look at how to save the file, compile and run the program. Please follow the steps given below:
  1. Open notepad and add the code as above.
  2. Save the file as: HelloWorld.scala.
  3. Open a command prompt window and go o the directory where you saved the program file. Assume it is C:\>
  4. Type 'scalac HelloWorld.scala' and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line.
  5. Above command will generate a few class files in the current directory. One of them will be called HelloWorld.class. This is a bytecode which will run on Java Virtual Machine (JVM).
  6. Now type 'scala HelloWorld' to run your program.
  7. You will be able to see 'Hello, World!' printed on the window.
C:\> scalac HelloWorld.scala
C:\> scala HelloWorld
Hello, World!


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments: on "Scala - Environment Setup"

Post a Comment