An Intro to Scala for Java Programmers (Part I)
Variables, Functions and Classes
This series of articles is aimed at programmers with a solid understanding of Java (or equivalent Object Orientated Language) looking for an overview of Scala. It isn’t a tutorial, more a delineation of some of the core separating points.
For those who aren’t familiar with Scala, the two languages are often mentioned simultaneously as they function on the JVM. Scala leverages this fact by allowing you to use Java code within a Scala program.
In our opening gambit we will explore classes, functions and variables. We won’t look into how to build and run projects, but examine the syntax to highlight the value of the language.
We also won’t delve into the ‘functional vs. object-oriented’ debate. We will assume you have a reasonable understanding of the matter, and that it may provide some of the impetus for your interest in Scala.
Hopefully the information will be helpful in letting you decide if Scala is a language you would be interested in learning.
In Java we declare variables as below.
Scala uses a slightly different syntax:
val keyword is for immutable variables, whereas
var is for mutable ones. Notice we don’t necessarily give the type, this is inferred by the compiler. However, in some coding styles it is considered best practice to explicitly declare it.
As Scala lends itself to functional programming it defaults to immutability, whereas Java uses the mutable option (although
final can be used to circumvent this).
Additionally Scala doesn’t allow the expression of primitives (such as
int) or static types. The latter was a design decision as they aren’t best object-oriented practice.
Later versions of Java have introduced similar concepts to
var, although Scala has implemented them from birth.
Another interesting feature of Scala is lazy evaluation of variables. This allows us to only evaluate an expression on its first use. Below we have an example of how this works. We require a function in order to do the demonstration, which we will cover in the following section. Try to follow along as best you can until then.
In Java we declare a function as below:
Scala is a little different.
Aside from the syntax, the core difference is that Java treats functions as objects (using functional interfaces). Scala treats them as variables.
In Java we implement a simple class and interface as below.
We have two important notions: static (to access this class member we do not need an object instance) and non-static (we require an object instance).
In Scala we look at classes slightly differently. Initially we need to introduce the notion of an ‘object’. This is shorthand for a singleton (i.e. we will only ever have one instance of this object).
Although objects in Scala initially resemble syntactic sugar for singletons, we need to introduce some further concepts in order to recognise their true power.
Let’s define a regular class in Scala, similar to our Java one.
You can see we use the
new keyword to create an instance of the class, and that this works very similarly to Java. Where Scala objects come into their own is with ‘companion classes’.
See how in the companion object we can put methods similar to static ones. Equally the companion object can access the private class members. They’re especially useful for things like factory methods.
Given a brief introduction to the notion of companion classes and companion objects, we can also introduce case classes.
The final thing we will touch on is Scala Traits vs Java 8 interfaces. Post Java 8 it was possible for interfaces to contain method definitions, meaning theoretically a Java class could pseudo-inherit multiple behaviours. However, this was seen as an anti-pattern.
In Scala there is a competing notion of mixing in traits. What we offer below is a very basic introduction serving only to whet your appetite. In later articles we can cover the true power of the pattern.
In conclusion, we have covered some of the more interesting basic separations between Scala and Java. In future articles we will look to extend on this theme.