It is true that “Scala is hard”, but the effort needed to learn it is worth it. Several advanced features of the language, such as http://www.tutorialspoint.com/scala/scala_tuples.htm, Functions, and Macros, can help developers write higher-quality code and [improve performance by coding in Scala. We are programmers, after all, and if we aren’t able to learn a language that has some complexity, then we’re in the wrong field.
Scala](http://www.scala-lang.org/) is a type-safe JVM language that blends object-oriented and functional programming into a very concise, logical, and extremely powerful language. Surprisingly, Scala is not as new as some may think, having been first released in 2003. However, it is in the past few years that [Scala has gained a substantial following. This leads to the question, “Why Scala?”.
This article will explore the benefits of Scala, particularly in comparison to Java (as Scala runs on the JVM). Scala isn’t the only language that attempts to be a “better Java”. Other languages like Kotlin and Ceylon have tried, but they chose to stay very close to Java’s syntax to make it easier to learn. While this might seem like a good idea, it ultimately forces developers to stay within some of the same Java paradigms that they wanted to move away from in the first place.
Scala, however, was designed from the ground up to be a better language, getting rid of the aspects of Java that were considered restrictive, overly long, or frustrating for developers. As a result, there are code distinctions and paradigm shifts that can make it a little harder to learn Scala programming at first, but the result is a cleaner and more well-organized language that is ultimately easier to use and boosts productivity.

Scala Compared to Java: Which One is Actually More Complex?
While Java’s simplicity has contributed to its success, it has also, ironically, contributed to its complexity. You can write almost anything in Java, but the amount of code needed can be intimidating. Programming in Scala, on the other hand, has a slightly steeper learning curve. However, if you can write a slightly more complex single line of code in Scala that can replace 20 “simpler” lines of Java code, which one is really more complex?
The reality is that Java can often be too verbose. Scala’s compiler is very intelligent and can infer many things, which prevents developers from having to explicitly specify them. For example, compare this simple “Hello World!” program written in both Java and Scala:
Hello World in Java:
| |
Hello World in Scala:
| |
Even in this basic example, Scala is more concise, despite the fact that there isn’t a significant difference between the two languages.
Let’s look at a more practical example: building a simple list of Strings:
Java:
| |
Scala:
| |
Although there are a few Java tricks that can be used to shorten the code, they aren’t commonly used.
Now, let’s say we have a list of strings that are numbers, and we want to turn it into a list of integers:
Java:
| |
Scala:
| |
This conversion is extremely easy thanks to Scala’s functional characteristics.
Java vs. Scala: A Class Example
Let’s take this a step further and compare standard bean / plain old Java object (POJO) creation in Java and Scala.
Let’s start with the Java version:
| |
That’s a lot of code.
Now for the Scala version:
| |
Didn’t we ask which language was more difficult?
Is This a Fair Comparison?
If you’ve gotten this far and are a Java programmer, you might be wondering if I’m comparing the two languages unfairly. After all, nothing is stopping me from making public variables in Java to get rid of the getters and setters.
However, if you recall the logic behind using getters and setters in Java, it was for future-proofing. This means that if you needed to add logic to either the getting or setting of variables in the future, you would have to rewrite those public variables to use methods instead (which is why using getters and setters in the first place is encouraged in Java). This is not the case with Scala programming. The abstraction is preserved without the use of getters and setters due to the language’s design. Consider the following modified User class in Scala, which throws a NullPointerException if you attempt to set the name to null:
| |
You can still set the name as follows:
| |
It is important to note that this eliminates the need to pre-configure method accessors entirely.
Furthermore, because Scala prefers immutability, I can write this much more succinctly using case classes:
| |
The amount of code I have to write is astounding.
Expanding on the Example
Consider the preceding classes, where I want to add a useful little method to the User class that returns a list of all Products that the User has ordered:
In Java’s verbose world:
| |
Fortunately, java.util.List has an addAll method; otherwise, getProducts() would have been even longer in Java.
In Scala, all we need is this:
| |
As you can see, the Scala language implementation is much smaller. Yes, it may appear more complicated to someone who is new to Scala, but once you grasp the concepts behind it, the Scala code will appear far simpler than the Java code.
Let’s make things a little more complicated. What if we only want to get Products that are in a specific Category?
In this case, we can’t use the addAll method in java.util.List, so things get messier in Java:
| |
In Scala, the code is still very straightforward. To combine the products lists from each Order flattened into a single list, we use flatMap. Then, we filter it down to only include the ones that belong in the category:
| |
Dynamic versus Static
While there has been no shortage of new languages in recent years, Scala is statically-typed, whereas nearly all others that have emerged are dynamic.
Although I know and use many dynamic languages, I believe that compile-time checks are essential for writing solid code as a professional developer. In a dynamic language, you can never be confident that your code is bug-free and robust until you’ve tested it in a variety of scenarios. This can result in potentially serious code flaws that are not discovered until the code is in production.
Conclusion
Hopefully, this article comparing Java to Scala has given you a basic understanding of Scala’s power and capabilities, as well as piqued your interest in learning the language. It is not only a fantastic language that can make programming less tedious and more enjoyable, but it is also becoming increasingly used by some of the largest companies in the world (LinkedIn, Twitter, FourSquare, and The Guardian, to name a few).
The popularity and use of Scala is rapidly increasing, as seen by the growing number of job openings for Scala developers. If you haven’t already, now is an excellent time to start riding the wave and stop wondering, “Why learn Scala?”