Saturday, December 15, 2012

Scala for Java 7 Developers: Overview

Target audience: Beginner
Estimated reading time: 10'

The purpose of this post is to introduce the Scala programming language from the perspective of an experienced Java developer. It is assumed that the reader has a good command of design patterns and multi-threaded applications.
In a nutshell, Scala is:
- Object-oriented
- Functional
- Scalable (Actors and immutability)

The elements of the Scala programming language presented in this post are a small subset of the rich set of features developers would benefit from the language.

Note: For the sake of readability of the implementation of algorithms, all non-essential code such as error checking, comments, exception, validation of class and method arguments, scoping qualifiers or import is omitted

Scala relies on immutable object and collections to reduce complexity associated with multi-threaded applications, eliminate race condition, simplify debugging.
The CRU (Create, Read, Update) object lifecycle in Java is reduced to CR (Create, Read) in Scala.
The Scala standard library offers a large variety of immutable collections.

val x = 23.5  // Read only
val mp = new immutable.Map[String, String]
val xs = List[Any]()

Traits vs. interface
Java interfaces are purely declarative. Scala traits allows:
* method implementation
* member value initialization
* abstract and lazy values (to be initialized through sub-class or on demand)
Scala traits cannot be instantiated and do not have constructors. Scala emulates multiple inheritance by stacking traits (Cake pattern).

trait MyObject[T] {
  val tobeDefined: Int
  def add(t: T): Option[T] = {}
  def head: T = { }

Lazy values and methods
Scala supports lazy values that are declared as member of a class but instantiated once and only once when they are accessed

class MyObject[T] {
  lazy val n = (n<<2) +

3rd party libraries and frameworks cannot always be extended through composition or inheritance. Scala implicit class/types and methods allows a "behind the scene" type conversion. The following code snippet converts a double value into a vector/array of double of size 1. The implicit is automatically invoke whenever it is imported into another section of the code base: for instance, the vector v is constructed through the implicit conversion dbl2Vec.

type DblVector = Array[Double]
  // Definition of implicit
implicit def dbl2Vec(x: Double): DblVector = Array.fill(1)(x)
val v: DblVector = 4.5

The following example extends the concept of Exception handling, Try with a concept of Either converting any instance of Try to an instance of Either using toEither method of the implicit class Try2Either. The implicit class conversion is used in handling a rounding error in the normalize function.

implicit class Try2Either[T](_try: Try[T]) {
  def toEither[U](r: ()=>U)(f: T=>T): Either[U, T] = _try match {
    case Success(res) => Right(f(res))
    case Failure(e) => Console.println(e.toString); Left(r())  

 * Method fails and recover if the denominator is too small
def normalize(x: DblVector): Either[Recover, DblVector] = Try {
  val sum = x.sum 
  if(sum > 0.01) _ / sum) 
  else throw new IllegalStateException("prob out of range")  
.toEither(() => new Recover)((v: DblVector) => Math.log(_)))

Tail recursion
Although convenient, traditional recursive implementation of computation can be ineffective, leaving developers to rely on iterative algorithms.
Scala's tail recursion is a very effective way to iterate through a data sequence without incurring a performance overhead because it avoids the creation of new stack frames during the recursion.
The following code snippet implements the Viterbi algorithm used to extract the most likely sequence of latent states in an hidden Markov model given a lambda model and a set of observations.

private def viterbi(t: Int): Double = {

  if( t == 0) initial   
  else { 
    Range(0, lambda.getN).foreach( updateMaxDelta(t, _) )   
    if( t ==  obs.size-1) {
        val idxMaxDelta = Range(0, lambda.getN).map(i => 
                    (i,, i))).maxBy(_._2)
        state.QStar.update(t+1, idxMaxDelta._1)
    else viterbi(t+1)

Actor model
Traditional multi-threaded applications rely on accessing data located in shared memory. The mechanism relies on synchronization monitors such as locks, mutexes or semaphores to avoid deadlocks and inconsistent mutable states. Those applications are difficult to debug because of race condition and incur the cost of a large number of context switches.
The Actor model addresses those issues by using immutable data structures (messages) and asynchronous (non-blocking) communication.
The actors exchange messages and data using immutable messages as described in the example below.

sealed abstract class Message(val id: Int)
case class Activate(i: Int, xt: Array[Double]) extends Message(i)
case class Completed(i: Int, xt: Array[Double]) extends Message(i)
case class Start(i: Int =0) extends Message(i)

An actor processes messages queued in its mailbox asynchronously, FIFO. The method, receive which returns a PartialFunction is the message processing loop, which correspond to the in Java

final class Worker(
     id: Int, 
     fct: DblSeries => DblSeries) 
extends Actor {
    // Actor event/message loop
  override def receive = {
    case msg: Activate => {
      val msgId =
      val output = fct(msg.xt)
      sender ! Completed(msgId, output)

Syntactic similarities

Java developers will find comfort knowing that Scala preserves a lot of constructs and idioms which make Java 7 such a wide spread and supported programming language.
Construct ScalaJava 7
Variable         var x: Float = 1.0F  float x = 1.0F;
Value val n = 4L
Constants final val MAX_TEMP: Float = 232.0F final float MAX_TEMP = 232.0F;
Class class Person { ...} public class Person { }
Constructor class Person(name: String, age: Int) {} public class Person {
....private String name;
....private int age;
....public Person(String name, int age) {}
Setters Not applicable
public class Person {
....public void setName(String name) {}
Getters class Person(val name: String, val age: Int{}
....//Public access to member values.
public class Person {
....public final String getName() {}
Method Arguments validation def add(index: Int): Unit = {
....require(index>=0 && index<size, .... s"$index out of bounds")
public void add(int index) {
....if( index < 0 || index >= size) {
........ throw new IllegalArgumentException(...);
Singleton object Scientist extends Person {} public class scientist extends Person {
.... private static Scientist instance = new Scientist();
.... private Scientist();
.... public static Scientist getInstance() {
........ return instance;
... }
Overriding override def getName(id : Int) : String {.. } @override
public final getName(int id) { ... }
Interfaces trait Searchable {
.....def search(id : Int) : String
interface Searchable {
....String search(int id);
Type Casting n : Int = o.asInstanceOf[Int] Integer n = (Integer)o;
Type Checking n.isInstanceOf[Int] n instanceOf Integer
Generics class Stack[T](val maxSize : Int) {
....def push(t : T) : Unit = { ... }
....def pop : T = { ... }
public class Stack <T> {
........private int maxSize;
........public Stack(int maxSize) { ... }
........public void push(T t) {.... }
........public <T> pop() {... }
Abstract Class abstract class Collection[T](val max : Int) {
....def add(t : T) : Unit
....def get(index : Int) : Int
abstract public class Collection <T> {
........abstract public void add(T t);
........abstract public <T> get(int index);
Exception Try { .... }
match { Success(res) =>{}
... case Failure(e) => {}
try { .. }
catch( NullPointerException ex) { ..}
catch( Exception ex) { ...}
Bounded type
class List[T <: Float] {  ... }
class List[T >: Node] { ...}
public class List<T extend Float> { ... }
public class List <T super Node> { .... }
class List[+T] {}
class List[-Node] { ...}
public class List<? extends T> { ... }
public class List<? super Node> { .... }
For loop
for( i <= 0 until n) { .. } for( int i = 0; i < n; i++) { .. }
for(index <- indices) { ... }for(int index : indices) { .. }

Key differences

There are quite a few important and tricky programmatic difference between Scala 2.11 and Java 7
  • == In Scala == compares instances similarly to equals. In Java == compares the reference to instances
  • Comparable integer java.lang.Integer supports comparable interface while scala.Int does not
  • Static method Java supports explicit static methods. Scala does not, although static methods are implicitly defined as method of objects (Singletons)
  • Inner class In Scala, Inner class are accessed through the reference Self to the outer class. Java accesses Inner class through the Outer class
  • Checked exceptions Scala does not support Checked exceptions, Java does.
  • Primitive types As a pure object oriented, Scala does not support primitive types. Java does
  • Expression as a value As a functional language, Scala defines expressions such as if ... else ... as value, that can be evaluated and assigned to. Java does not
  • Immutable collections Scala supports immutable collections by default. Java does not.
  • Localized imports and type aliases Scala has scoped import and type aliases. Java does not
  • Explicit operator overloading Scala allows developers to overload operator through the definition of new methods. Java does not.
  • Actor Scala provides full parallelism with actor and immutable messages. Java does not.
  • Futures Scala futures are asynchronous and can be updated thorough promises. Java does not support the concept
  • Higher order kind Scala has higher order kind, such as Monad. Java does not
  • Tail recursion (or elimination) Scala supports tail recursion in order to avoid stack overflow. Java does not
  • Monad and functors are core concept in Scala, but not in Java.

Note This comparative analysis relies on version 2.11.x of Scala language and Java 7. Future version of Scala and Java may produce a slightly different result.

So much more....
This post barely scratches the surface of the capabilities of Scala which include
  • Futures
  • Partial functions and partially applied functions
  • Dependency injection
  • Monadic representation
  • Macros
  • View bounded type parameterization
  • F-Bounded polymorphism
  • Closures
  • Delimited continuation
  • Method argument currying



  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

  2. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

  3. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

  4. I can only express a word of thanks! Nothing else. Because your topic is nice, you can add knowledge. Thank you very much for sharing this information.

    Avriq India
    pest control
    cctv camera

  5. you will need support or suggestions, write me privately.
    I interested in your implementation/use case.
    the best situs slot terbaik