Tuesday 11 April 2017

Scala Overview

Scala Overview

Scala, short for Scalable language, is a functional programming language. Scala integrates features of object-oriented and functional languages and it is compiled to run on the Java Virtual Machine.

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 which will be explained in subsequent chapters.
Classes are extended by subclass and a flexible composition mechanism as a clean replacement for multiple inheritance.

Scala runes on JVM

Scala is compiled into Java Byte Code, which is executed by the Java Virtual Machine (JVM). This means that Scala and Java have a common run-time 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.

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.

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.

Java 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 features which is differ from 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 is being used everywhere and importantly in enterprise web applications. You can check few of the most popular Scala web frameworks:
  • The Lift Framework.
  • The Play framework.
  • The Bowler framework.

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 make sure that you have Java 1.5 or greater installed on your computer.

Installing Scala on Windows


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, do following.

C:\>java -version 
java version "1.7.51" 
Java(TM) SE Runtime Environment (build 1.7.51-b51) 
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b05, mixed mode) 
C:\>

Test to see that the Java compiler is installed. Type javac -version. You should see something like the following:

C:\>javac -version javac 1.7.51 
C:\> 

Scala Setup:

you can download Scala from http://www.scala-lang.org/downloads. I am trying with scala-2.9.0.1-installer.jar and put it in C:/> directory. Execute the following command at command prompt:

C:\>java -jar scala-2.9.0.1-installer.jar 
C:\>

This will display an installation wizard, which will guide you to install scala on your 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.

C:\>scala -version 
Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL 
C:\>

Installing Scala on Linux


Java Setup:

Make sure you have Java JDK 1.7 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.7.51" 
Java(TM) 2 Runtime Environment, Standard Edition (build 1.7.51-b03) 
Java HotSpot(TM) Server VM (build 1.7.51-b03, mixed mode) 
$

Test to see that the Java compiler is installed. Type javac -version.

$javac -version 
javac 1.5.0_22 
javac: no source files 
Usage: javac <options> <source files> 
................................................ 
$

Scala Setup:

you can download Scala from http://www.scala-lang.org/downloads. I am trying with scala-2.9.0.1-installer.jar and put it in /tmp directory. 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 
................................................ 
[ Starting to unpack ] 
[ Processing package: Software Package Installation (1/1) ] 
[ Unpacking finished ] 
[ Console installation done ] 
$

This will display an installation wizard, which will guide you to install scala on your 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.

$scala -version 
Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL 
$

Scala basic

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.

  • Object - Objects have states and behaviors. Example: A cat has states - color, name as well as behaviors - eating. An object is an instance of a class.
  • Class - A class can be defined as a blueprint that describes 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

2 comments: