Scala combines the object-oriented and functional programming paradigms, using a concise syntax that is fully compatible with Java and runs on the JVM. Support for the functional programming style, and in particular Lambda expressions which are not now expected to be added to Java until JavaSE 8 in 2012, can help reduce the amount of boiler plate code that you are required to write, arguably making it easier to focus on the task in hand. This article provides an introduction to Scala.

To get started, just install the latest Scala distribution of the Typesafe stack, open a command prompt and type ‘scala’: this will start the REPL (read-eval print loop) interactive programming environment. You are now ready to enter your first Scala line:

We have just declared a variable of type Int with value 1492, just as we would do in Java with " Int columbus = 1492;". The difference here apart from the reverse syntax of putting the type after the variable in Scala is that the "val" explicitly declares the variable as immutable. If we try to modify it:

Notice how the message precisely shows where the error lies in the line. Try declaring the variable again, but this time as "var" to make it mutable. By the way, the compiler is smart enough to know that 1492 is an integer and therefore you do not need to specify the type at all:

We have defined a class Employee with 3 immutable fields called name, age and company with default values. The "case" word is analogous to the switch statement in Java, though more flexible. It means that the class has an extra mechanism for pattern matching, as well as other things, amongst which is a factory method to create instances (no need to use the "new" keyword to create one). Similarly there is no need to create default getters. Unlike Java the variables are public by default (not protected) and Scala creates a getter for public variables named after the name of the variable itself. You could make fields mutable and/or private if you wanted to by using "var" in front of the parameters (e.g. case class Person(private var name:String)).

Let’s create some instances in different ways to exhibit various features such as named and default arguments (available since Scala 2.8):

Scala collections systematically distinguish between immutable and mutable collections, but encourage immutability by constructing immutable collections by default. They simulate additions, updates or removals by returning new collections from such operations instead of modifying them.

This "for" loop construct is very close to Java's imperative style of programming. Another way to write it in Scala (and many languages on the JVM such as Groovy, JRuby or Jython) involves a more functional style, using lambda expressions (sometimes referred to as closures). In brief Lambdas are just functions that you can pass around as parameters. These functions take parameters as input (in our case the "n" integer) and return as their result the last statement of their body. They are in the form

In that case the body only has one statement (println...) and therefore returns Unit i.e. an "empty result" roughly equivalent to void in Java except "void" does not return anything.

Instead of just printing out our list of numbers, let’s say we want to manipulate and transform the elements; in that case we want to invoke methods that will produce a resulting list that we can reuse later on. Let’s try some examples:

This last transformation "map" is very useful; it applies the closure to each element of the list and its result is a list of the same size containing every transformed element.

One last method we would like to introduce here is the "foldLeft" method, that propagates state from one element to the next. For instance, to sum elements in a list you need to accumulate them and keep track of the intermediate counter from one element to the next:

The value 0 given as the first argument to foldLeft is the initial value (which means total=0 when applying the function for the first list element). The notation (total,element) represents a Tuple2 ,which is in Scala a tuple with 2 elements (e.g. to represent 3D space coordinates it is often useful to refer to a Tuple3 (x,y,z) etc...). Note that for summation the Scala API actually provides a "sum" method so the last statement could have been written:

scala> val sumOfNumbers = numbers.sumsumOfNumbers: Int = 6

There are many more of these collection transformation methods that you can check from the scaladoc API . You can also chain these methods (e.g numbers.reverse.filter...) to get more concise code, although it can affect readability.

Finally, a shorter notation equivalent to { n => n + 10 } exists in the form of (_ + 10), which means you do not have to declare the input parameter if it is just implied by the method you’re invoking; in our case "n" is called an anonymous variable because you can call it anything you like such as "x" or "number", so underscore means a blank you need to fill up with each element of your collection. (Groovy reserves the word "it" instead of _ , and Python uses "self").

Imagine the collection of allEmployees we have is the result set we got from an SQL query similar to "SELECT * FROM employees WHERE company = ‘DevCode’ ". Now we can sort the employees by company by transforming our List[Employee] into a Map where the key is the company name, and the value is a list of all employees who belong to that company:

The "case(key,value) " is an example of the very powerful pattern matching mechanism that Scala provides. See the Scala documentation for further explanations.

Now we’re done. What we have just written is a little "Map Reduce" algorithm. Since the aggregation of Employees for each company is totally independent of other companies, this algorithm is very straightforward to parallelize.

Equivalent implementations of the algorithm, in both Java and Scala, are given in the appendix.

About the Author

Thomas Alexandre is a senior consultant at DevCode, specializing in Java and Scala software development. He is passionate about technology, enthusiastic about sharing knowledge and always looking for ways to code more effectively through the adoption of new open-source software and standards. In addition to 14 years of Java development, he has focused these past few years on emerging languages and web frameworks such as Groovy/Grails and Scala/Lift. Thomas has a PhD in Computer Science from the University of Lille, France, and has spent 2 years as postdoc with Carnegie Mellon University in the fields of Security and E-Commerce.

Is your profile up-to-date? Please take a moment to review and update.

Email Address

Note: If updating/changing your email, a validation request will be sent

Company name:

Keep current company name

Update Company name to:

Company role:

Keep current company role

Update company role to:

Company size:

Keep current company Size

Update company size to:

Country/Zone:

Keep current country/zone

Update country/zone to:

State/Province/Region:

Keep current state/province/region

Update state/province/region to:

Subscribe to our newsletter?

Subscribe to our architect newsletter?

Subscribe to our industry email notices?

You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.