Friday, August 20, 2010

Scala Vs Java

Form last few days i'm trying to look in to some other dynamic language and trying to compare against java, Scala seems to be the better choice in compare to ruby and others...lets talk about Scala in this post and a small code comparison between Scala and java .. to start with, Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.

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. Class abstractions 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. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala's case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages.

Scala is statically typed: Scala is equipped with an expressive type system that enforces statically that abstractions are used in a safe and coherent manner.

Scala is extensible: The design of Scala acknowledges the fact that in practice, the development of domain-specific applications often requires domain-specific language extensions. Scala provides a unique combination of language mechanisms that make it easy to smoothly add new language constructs in form of libraries:

any method may be used as an infix or postfix operator, and closures are constructed automatically depending on the expected type (target typing).


Scala interoperates with Java and .NET: Scala is designed to interoperate well with popular programming environments like the Java 2 Runtime Environment (JRE) and the .NET Framework (CLR). In particular, the interaction with mainstream object-oriented languages like Java and C# is as smooth as possible. Scala has the same compilation model (separate compilation, dynamic class loading) like Java and C# and allows access to thousands of high-quality libraries.

Advantages of Scala:

Scala language has been designed to be scalable. It scales according to user needs. The object oriented concepts allows this language to be used for larger and more complex projects while the functional constructs allows programmer to develop short and concise code.

Portability: Scala runs on Java Virtual machine (JVM) this ensures the portability to all the underlying platforms. Also there is an initiative for building Scala on top of .NET Common language Runtime (CLR).

Performance: Scala generate more efficient java code which leads to better runtime performance.

Every value in Scala is an object and every operation is a method call. It is closer to object oriented programming than functional programming and therefore does not support concepts like static fields and methods. Scala allows you to define own objects and constructs which makes it possible to scale up according to user needs.

Scala uses strict typing. But it also allows most of the typing unspecified. When information is unspecified compiler will do smart type interface to infer information from code.

It incorporates ideas from many languages such as Mixins (a way to address multiple inheritances in LISP) and actors (used message passing communication architecture).

Java echo system is available for Scala.
Scala can be embedded into XML since its syntax is somewhat similar to that of XML
New design paradigms become available (working with closures, traits).
Learning speed for Scala is faster than that of C++ or Java for same task.
IDEA (IntelliJ), NetBeans and Eclipse all has good support for Scala.

Disadvantages of Scala:

Inferior quality of the documentation
Native libraries (Scala uses Java or .Net libraries as base for their own)
IDE and debugging support is not as good as Java.
Non-existing localization of documentation
It requires larger syntax to learn.
It consistently breaks backwards compatibility.

Skilled programmers are required to use the features of the language.

It is still in early adopter phase.
Lack of extensive tutorials

Now lets talk about small code and see, how they differ from each other..

A Better OOP Language

Scala works seamlessly with Java. You can invoke Java APIs, extend Java classes and implement Java interfaces. You can even invoke Scala code from Java, once you understand how certain “Scala-isms” are translated to Java constructs (javap is your friend). Scala syntax is more succinct and removes a lot of tedious boilerplate from Java code.

For example, the following Person class in Java:

class Person {

private String firstName;

private String lastName;

private int age;

public Person(String firstName, String lastName, int age) {

this.firstName = firstName;

this.lastName = lastName;

this.age = age;



public void String getFirstName() { return this.firstName; }


public void String getLastName() { return this.lastName; }


public void int getAge() { return this.age; }


can be written in Scala thusly:

class Person(var firstName: String, var lastName: String, var age: Int)

Yes, that’s it. The constructor is the argument list to the class, where each parameter is declared as a variable (var keyword). It automatically generates the equivalent of getter and setter methods, meaning they look like Ruby-style attribute accessors; the getter is foo instead of getFoo and the setter is foo = instead of setFoo. Actually, the setter function is really foo_=, but Scala lets you use the foo = sugar.

Lots of other well designed conventions allow the language to define almost everything as a method, yet support forms of syntactic sugar like the illusion of operator overloading, Ruby-like DSL’s, etc.

You also get fewer semicolons, no requirements tying package and class definitions to the file system structure, type inference, multi-valued returns (tuples), and a better type and generics model.

One of the biggest deficiencies of Java is the lack of a complete mixin model. Mixins are small, focused (think Single Responsibility Principle ...) bits of state and behavior that can be added to classes (or objects) to extend them as needed. In a language like C++, you can use multiple inheritance for mixins. Because Java only supports single inheritance and interfaces, which can’t have any state and behavior, implementing a mixin-based design has always required various hacks. Aspect-Oriented Programming is also one partial solution to this problem.

The most exciting OOP enhancement Scala brings is its support for Traits, a concept first described here and more recently discussed here. Traits support Mixins (and other design techniques) through composition rather than inheritance. You could think of traits as interfaces with implementations. They work a lot like Ruby modules.

Now look at one design patter example and comparing these two language.

Here is an example of the Observer Pattern written as traits, where they are used to monitor changes to a bank account balance. First, here are reusable Subject and Observer traits.

This trait looks exactly like a Java interface. In fact, that’s how traits are represented in Java byte code. If the trait has state and behavior, like Subject, the byte code representation involves additional elements.

trait Observer[S] {

def receiveUpdate(subject: S);


trait Subject[S] {

this: S =>

private var observers: List[Observer[S]] = Nil

def addObserver(observer: Observer[S]) = observers = observer :: observers

def notifyObservers() = observers.foreach(_.receiveUpdate(this))


In Scala, generics are declared with square brackets, [...], rather than angled brackets, <...>. Method definitions begin with the def keyword. The Observer trait defines one abstract method, which is called by the Subject to notify the observer of changes. The Subject is passed to the Observer.

There are lots of other differences between these languages, and I hope with small post will help others. who are trying to evaluate some other language, or trying to see whats out there....

No comments:

Post a Comment