From Java to Java.Next – Quick Review of Scala

To start this quick review about Scala as Java.Next, let us see an interesting perceptive observation from Martin Fowler about Java and its legacy as follows.

“The legacy of Java will be the platform, not the language”

This statement is interested since more than 200 languages run on Java Virtual Machine (JVM) nowadays [1]. This condition emerges since Oracle hosted the annual JVM Language Summit in 2008, which has given implementers of alternative languages on the JVM an opportunity to collaborate openly with Java platform engineers. For example, let us consider Clojure, Groovy, Scala, and the new one, Kotlin. Each language brings interesting new capabilities beyond of Java language itself. In general, two initial significant advantages that these languages tend to provide are reducing the amount of verbosity in code. Hence it allows us to focus on the essence of problem solving. The second advantage is enabling some degree of functional programming styles [2]. This short review will be limited to Scala as an alternative programming language of Java.

As introduced in their official website [3], Scala is an elegant and concise programming language that integrates both functional and object-oriented paradigms smoothly. This enabling programmers to be more productive while retaining full interoperability with Java and taking advantage of modern multicore hardware. It is statically typed and well found with an expressive type system, which supports: generic classes, variance annotations, upper and lower type bounds, inner classes and abstract types as object members, compound types, explicitly typed self references, views, and polymorphic methods.

Since Scala runs on JVM which maintains a large amount of interoperability with Java, it aims to make substantial improvements on Java. Mostly by reducing verbosity and introducing a variety of functional programming elements. In additions, Java developers do not have to give up their existing work if they want to start using Scala in projects. There are at least three major improvements that Michael Pigg from Charlot Solutions stated regarding Scala over Java. The first two are appropriate with two advantages we describe earlier. They are in increasing productivity due to reduced verbosity and developers can mix and match imperative and functional programming paradigm as needed. Whereas the last one is built in functionality for parallelism and concurrency [4].

As well as in Java, Scala has built-in support for classes parameterized with types. It is useful for the development of collection classes [5]. Unlike in Java that has use-site variance annotations, Scala has declaration-site variance [6]. It also has exceptions handling system like in many programming languages. However, Scala does not actually have checked exeptions like in Java. As in Java, Scala uses a try{ … } catch { … } block, except that the catch block uses matching to identify and handle the exceptions [7].

One example of huge project that implements this language is Twitter. Much of its infrastructure is written in Scala [8]. Why not Java? Alex Payne, one engineering who involved in this project answers this question [9]. First of all, they really took Scala’s advantage of better concurrency. Twitter wanted to do more with fewer machines. They wanted to be compiled so it is not burning CPU doing the wring stuff. Secondly, it is very idiomatic, functional, and fast. Its community is small but growing and accessible. They also like the grand unified theory of Scala, which is combines OOP and functional programming. Moreover, they use traits, pattern-matching, and mutability.

After seeing some advantages of Scala over Java, It is reasonable to consider Scala as first alternative programming language in learning programming. However, since Scala is running on JVM and maintaining interoperability with Java, we should also learn Java side-by-side as a review in learning Scala.

Finally, below is the simple example taken from [10] to illustrate the advantages of Scala over Java language.

Scala:

class Person(val firstName: String, val lastName: String)

Java:

public class Person {
    private final String firstName;
    private final String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}

References:

  1. http://www.ibm.com/developerworks/java/library/j-jn1/index.html accessed in 21/05/2013
  2. http://thinkrelevance.com/blog/2008/09/24/java-next-overview accessed in 21/05/2013
  3. http://www.scala-lang.org/node/25
  4. http://www.techrepublic.com/blog/programming-and-development/why-java-developers-should-check-out-scala/5753
  5. http://www.scala-lang.org/node/113
  6. http://stackoverflow.com/questions/10572403/about-generics-in-java-and-scala
  7. http://www.tutorialspoint.com/scala/scala_exception_handling.htm
  8. http://twitter.github.io/effectivescala/
  9. http://blog.redfin.com/devblog/2010/05/how_and_why_twitter_uses_scala.html
  10. 10. http://stackoverflow.com/questions/2952732/samples-of-scala-and-java-code-where-scala-code-looks-simpler-has-fewer-lines/2953040

2 Replies to “From Java to Java.Next – Quick Review of Scala”

  1. A good code example would have been nice. The example you provided is weak and doesn’t show much of anything. Ironically, the Java code you are comparing Scala to is auto-generated by IDEs these days.

Leave a Reply