Sunday, November 23, 2014
This article provides a quick introduction to Scala programming language.

What is Scala?

  • Scala is acronym for "Scalable Language".
  • Scala integrates object-oriented and functional language concepts.
  • Scala is often referred to as object-functional programming language.
  • Scala source code gets compiled to Java byte-code and runs on the Java Virtual Machine (JVM).
  • Java and Scala classes can be easily mixed. Java libraries can be used in Scala code and vice versa.
  • Scala design decisions have been made to address some of the shortcomings of Java.
  • Scala can also run on Android.

Hello Scala

Let us quickly jump into our first Scala program.

object SayHello {
  def main(args: Array[String]) {

To compile this program use scalac, the Scala compiler. This is similar to using "javac" for Java programs.
scalac SayHello.scala

To run this program use scala command. This is similar to using "java" command for Java programs.
scala -classpath . SayHello

Refer to Quick Start with Scala for Scala installation steps.

Some of the obvious differences to a Java programmer when looking at this Scala program:
  • Scala doesn't require semicolons to end statements.
  • Scala uses object keyword to create singleton objects. There is no class declaration or a static declaration. Scala doesn't have static variables or methods.
  • Scala needs def keyword to precede function definitions.
  • Scala doesn't need an explicit access modifier and by default visibility is public. 
  • In Scala parameter types follow the variable name unlike Java.

Java interaction

Let us look at our second example where we use Java libraries inside our Scala code. In this example, we print the current date using java.util.Date.

import java.util.Date

object UseJavaAPI {
  def main(args: Array[String]) {
    var today = new Date
    println(today toString)

Some of the obvious differences to a Java programmer when looking at this Scala program:

  • Scala uses var keyword to declare variables.
  • Scala supports variable type inference based on the value assigned to it. In this example, we have not specified the type for variable "today" and is inferred to be Date type based on the value assigned. Also it is important to note that Scala is a statically typed language.
  • Scala has syntactic flexibility. For example, to invoke toString() function on variable today as "today.toString()" it would suffice to write "today toString".
There are several other interesting features in Scala which we explore in rest of the articles. These include:
  • Type inference
  • Lazy evaluation
  • Functions are objects
  • Mixin based composition
  • Traits
  • Closures
  • Tuples
  • Higher order functions
  • Currying
  • Nested functions
  • etc.
Hope this article gives a short introduction to understand basic Scala programs. Proceed to read other other Scala tutorials.

Contact Form


Email *

Message *

Back to Top