Introduction to Scala: Part 1

What is Scala?

Scala is an open-source language that runs on the JVM. It was developed at the Programming Methods Laboratory of the Swiss Federal Institute of Technology (EPFL), mainly by Martin Odersky (a co-designer of Java generics).

TypeSafe, a private company, offers training, commercial maintenance, support, and operations tools for Scala, as well as Akka and Play.

The Scala website has the following description of the language:

Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application. -

Hello World

Here’s a simple HelloWorld example in Scala:

object HelloWorld {
 def main(args: Array[String]) {
  println(“Hello, world!”)

You’ll notice that HelloWorld is declared as an object. This is similar to the keyword static in Java, except that instead of putting the keyword on every method and field, you just wrap everything up in one block. Since the main method in Java is static, we need to put the main method in an object in Scala.

The def keyword is used to define functions.

The next thing you might notice is that types are declared after the parameter name. In Scala type declarations always come on the right-side after a colon.

Array[String] is the equivalent of String[] in Java. Everything in Scala is an Object, including arrays.

Lastly, you’ll see that the println function is called which is equivalent to System.out.println in Java.

Everything’s an object

Scala has no primitives as in Java. In other words, when coding in Scala, everything is an object. However, the compiler will compile your code down to primitive math if possible, so you don’t lose performance. In addition, functions are considered first-class citizens in Scala (they can be passed around as values), which is why they say Scala combines the features of object-oriented and functional languages.

To achieve this feat, everything in Scala extends from either AnyVal or AnyRef. AnyRef is the equivalent of Object in Java, but AnyVal is the super-class of all primitive values - although it appears to be an object while coding.

Defining objects in Scala is dead-simple and devoid of much of the ceremony involved in Java. Essentially, the definition of the constructor also defines the fields of the class. Combined with the fact that everything is public by default, and that the val or var keyword is used to define a field, and class definitions are very short.

For example, the following one-line class definition defines a Person class with a nameand age with accessors and modifiers:

  class Person(var name:String, var age:Int)

The keyword “var” means variable, while “val” (an immutable value) is used more frequently in the functional programming style.

Everything’s an expression

Unlike in Groovy and most other languages, everything is Scala is an expression. For example, the following code snippet defines a method called eatDonuts:

def eatDonuts = println(“yum”)

Notice the lack of curly brackets.

Also, since everything is an expression, everything has a return value, replacing the need for the return keyword. For example, you might see the following method in Scala:

def toString = if (number < 0) “-” + (-number) else “+” + number

This also means a ternary expression (exp ? “true” : “false”) would be redundant in Scala.

Scala: Part 2

Tags: Scala