Alternative JVM languages: Scala explained
The world of Java application development is not restricted to just the core Java language and the Java Enterprise Edition platform. The Java virtual machine (JVM) is host to a variety of other languages like Scala, Groovy and Clojure, each with their own advantages and disadvantages. In this video, Java guru Craig Tataryn of the Basement Coders podcast explores one such JVM alternative language -- Scala.
There are many reasons to consider branching out from Java EE or Java SE into one of the many available alternative languages on the JVM. Some developers complain that Java has gotten a bit stagnant. Tataryn says, "This is false(ish)." He refutes the idea that Java EE is still overly verbose. Taking a look at the diamond syntax shows how to cut down type declarations and reduce the ugliness of Java syntax.
Another point against Java is that there are too many frameworks. Hibernate, Spring, cglib and others are all worth considering. Tartaryn admits that "learning Java is not the hard part. It's learning when and how to use the frameworks surrounding it, and there's no shortage of frameworks when it comes to Java application development."
Then there's the lack of language features such as mixins, first-class functions, higher order functions, lambdas, type inference, and a hybrid approach between dynamic and static typing. Mixins let you mix functionality into a class that wasn't declared with that functionality when it was created. First-class functions are baked into the language as their own types, instead of being generic classes.
Higher order functions allow developers to pass abilities to another function or to return other functions; it can be done in Java, but the function has to be wrapped in a class. Lambdas let developers declare a set of functionality or a code block that can be referred to repeatedly by other functions. Type inference is the ability of the compile to guess the appropriate variable types based on context in the code.
If these shortcomings with the core Java language are too much, Java developers may want to consider an alternative like Scala. Scala offers a great deal of interoperability with Java. This is a huge advantage coming into Scala from Java. Scala is statically typed, it's object oriented, Scala syntax is similar to Java and inheritance works in Java.
Scala is also a functional programming language with both higher order functions and first-class functions. Immutability rules are relaxed in Scala, which eases multithreaded processes. Scala also uses persistent data structures that blend the functional benefits of immutability when possible with preferment mutability where necessary. A list that can have extra elements added to the list is a prime example of a persistent data structure.
In Scala, mixins are implemented by creating traits. A trait in Scala is like an interface in Java except that not all functions on a trait will be abstract. Some may have implementation behaviors. With mixins you can mix in several traits, and you don't have to pick those classes right away. Scala also has the concept of implicits. Implicit methods can be defined to smooth over compile time errors where one type does not match another type.
Java 8 will help by giving Java developers lambdas, type inference and other important features. However, developers who don't have the patience to wait for Java 8 should consider switching to a JVM alternative such as Scala. "It does not hurt to learn one of these other languages," Tataryn concludes. "You're going to learn so much, and it will only be beneficial."
09 Apr 2013