Scala is new generation JVM language, which is generating popularity as alternative of arguable one of the most popular language Java. It's not yet as popular as Java, but slowly getting momentum. As more and more Java developers are learning Scala and inspired by Twitter, more and more companies are using Scala, it's future looks very bright. To start with, Scala has several good feature, which differentiate it from Java, but same time it has lot of similarities as well e.g. both Scala and Java are JVM based language, You can code Scala in Java way and Scala can use any Java library, which in my opinion a great decision made by designers of Scala. Since tremendous works has already been done in form of open source framework and library in Java, it's best to reuse them, rather than creating a separate set for Scala. Out of several differences, one of the main difference between Scala and Java is it's ability to take advantage of Functional programming paradigm and multi-core architecture of current  CPU. Since current CPU development trend is towards adding more cores, rather than increasing CPU cycles, it also favors functional programming paradigm. Though this differences may not be significant, once Java 8 will introduce lambdas, but it might be too early to comment. Apart from functional programming aspect, there are many other differences as well. One of the obvious one is improved readability and succinct code. Java is always on firing line for being too verbose, I thing Scala does take care of that and code which took 5 to 6 lines in Java, can be written in just 2 to 3 lines in Scala. Well Grounded Java Developer has some nice introduction on JVM languages like ScalaGroovy and Closure, which is worth reading.  In this article, we will see such kind of similarities and differences between Scala and Java.

Similarities between Scala and Java

Following are some of the major similarities between Scala and Java programming language :

1) Both are JVM based language, Scala produce same byte code as Java and runs on Java Virtual Machine. Similar to Java compiler javac, Scala has a compiler scalac, which compiles Scala code into byte code. At this level, all JVM language like GroovyJRubyScala becomes equals to Java, because they use same memory space, type system and run inside same JVM.

2) You can call Scala from Java and Java from Scala, it offers seems less integration. Moreover, you can reuse existing application code and open source Java libraries in Scala.

3) Major Java programming IDE like Eclipse, Netbeans and InetelliJ supports Scala.

4) One more similarity between Scala and Java is that both are Object Oriented, Scala goes one steps further and also supports functional programming paradigm, which is one of it's core strength.

Differences between Scala and Java

1) First and Major difference you will notice between Scala and Java is succinct and concise code. Scala drastically reduce number of lines from a Java application by making clever use of type inference, treating everything as object, function passing and several other features.

2) Scala is designed to express common programming patterns in elegant, concise and type-safe way. Language itself encourage you to write code in immutable style, which makes applying concurrency and parallelism easily.

3) One difference, which some might not notice is learning curve. Scala has steep learning curve as compared to Java, my opinion may be slightly biased because I am from Java background, but with so much happening with little code, Scala can be really tricky to predict. Syntax of Scala looks confusing and repulsive as compared to Java, but I am sure that is just the starting hurdle. One way to overcome this hurdle is following a good Scala book like  Programming in Scala or Scala in Action, both are excellent books for a Java developer, who wants to learn Scala

4) One of Scala's cool feature is built-in lazy evaluation, which allows to defer time consuming computation, until absolutely needed and you can do this by using a keyword called "lazy" as shown in below code :
// loading of image is really slow, so only do it if need to show image
lazy val images = getImages()  //lazy keyword is used for lazy computation

    // Do something without loading images.

If you love to learn by following examples, then I guess Scala CookBook is a another good buy, contains tons of examples on different features of Scala.

5) Some one can argue that Java is more readable than Scala, because of really nested code in Scala. Since you can define functions inside function, inside another functions, inside of an object inside of a class. Code can be very nested. Though some time it may improve clarity, but if written poorly it can be really tricky to understand.

6) One more difference between Scala and Java is that Scala supports Operator overloading. You can overload nay operator in Java and you can also create new operators for any type, but as you already know, Java doesn't support Operator Overloading.

7) Another major difference between Java and Scala is that functions are objects in Java. Scala treats any method or function as they are variables. When means, you can pass them around like Object. You might have seen code, where one Scala function is accepting another function. In fact this gives the language enormous power.

8) Let's compared some code written in Scala and Java to see How much different it look:


List<Integer> iList = Arrays.asList(279810);
List<Integer> iDoubled = new ArrayList<Integer>();
for(Integer number: iList){
    if(number % 2 == 0){
        iDoubled.add(number  2);


val iList = List(279810);
val iDoubled = iList.filter(_ % 2 == 0).map(_  2)

You can see that Scala version is lot succinct and concise than Java version. You will see more of such samples, once you start learning functional programming concepts and patterns. I am eagerly waiting for Scala Design Patterns: Patterns for Practical Reuse and Design by John Hunt, which is not yet released and only available for pre order. This book is going to release this month.

That's all on this article about similarities and differences between Scala and Java.  Though they are two separate programming language, they have lot in common, which is not a bad thing at all and in my opinion that's the only thing, which will place Scala as Java alternative, if at all it happens in future. As I had mentioned in my post 10 reason to learn Java programming, that Java tools, libraries, and community is it's biggest strength and if Scala can somehow reuse that, it will be well ahead, forget about competing though, it will take years to build such community and code. For a Java programmer, I would say nothing harm in learning Scala, most likely you will learn few good practices, which you can even apply in Java, as corporate sector is still on Java, and Scala in it's early days, you can be well ahead, if you learn Scala now. On closing note, at high  level Scala looks very promising, all design decision made are really good and they came after several years of experience with Java.

Recommended Books on Scala for Java Programmers
Books are best way to learn a new programming language, first of all they contains complete information but also in much more readable and authentic form. I strongly recommend to follow at-least one book, before jumping on blogs and online articles. One reading any Scala Programming book is must to build fundamental, which is indeed necessary, given rather steep learning curve of Scala. Following are my list of some books from which you can choose one.

Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition by Martin Odersky, Lex Spoon and Bill Venners
Scala for the Impatient by Cay S. Horstmann
Scala in Depth by Joshua D. Suereth and Martin Odersky

Thanks folks, Enjoy learning Scala.


Post a Comment

Mithilesh Joshi