Scala ( ) is a strongly statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala's design decisions are intended to address criticism of Java.
Scala source code can be compiled to JVM bytecode and run on a Java virtual machine (JVM). Scala can also be transpiled to JavaScript to run in a browser, or compiled directly to a native executable using Clang. When running on the JVM, Scala provides language interoperability with Java so that libraries written in either language may be referenced directly in Scala or Java code. Like Java, Scala is object-oriented, and uses a syntax termed curly-brace which is similar to the language C. Scala 3 added an option to use the off-side rule (indenting) to structure blocks, and its use is advised. Martin Odersky has said that this turned out to be the most productive change introduced in Scala 3.
Unlike Java, Scala has many features of functional programming languages (like Scheme, Standard ML, and Haskell), including currying, immutability, lazy evaluation, and pattern matching. It also has an advanced type system supporting algebraic data types, covariance and contravariance, higher-order types (but not higher-rank types), anonymous types, operator overloading, optional parameters, named parameters, raw strings, and an experimental exception-only version of algebraic effects that can be seen as a more powerful version of Java's checked exceptions.
The name Scala is a portmanteau of scalable and language, signifying that it is designed to grow with the demands of its users.
History The design of Scala started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) (in Lausanne, Switzerland) by Martin Odersky. It followed on from work on Funnel, a programming language combining ideas from functional programming and Petri nets. Odersky formerly worked on Generic Java, and javac, Sun's Java compiler.
After an internal release in late 2003, Scala was released publicly in early 2004 on the Java platform,Martin Odersky, "The Scala Language Specification Version 2.7" A second version (v2.0) followed in March 2006.
On 17 January 2011, the Scala team won a five-year research grant of over €2.3 million from the European Research Council. On 12 May 2011, Odersky and collaborators launched Typesafe Inc. (later renamed Lightbend Inc.), a company to provide commercial support, training, and services for Scala. Typesafe received a $3 million investment in 2011 from Greylock Partners.
Platforms and license Scala runs on the Java platform (Java virtual machine) and is compatible with existing Java programs. As Android applications are typically written in Java and translated from JVM bytecode into Dalvik bytecode (which may be further translated to native machine code during installation) when packaged, Scala's Java compatibility makes it well-suited to Android development, the more so where functional programming is preferred.
The reference Scala software distribution, including compiler and libraries, is released under the Apache license.
Other compilers and targets Scala.js is a Scala compiler that compiles to JavaScript, making it possible to write Scala programs that can run in web browsers or Node.js. The compiler, in development since 2013, was announced as no longer experimental in 2015 (v0.6). Version v1.0.0-M1 was released in June 2018 and version 1.1.1 in September 2020.
Scala Native is a Scala compiler that targets the LLVM compiler infrastructure to create executable code that uses a lightweight managed runtime, which uses the Boehm garbage collector. The project is led by Denys Shabalin and had its first release, 0.1, on 14 March 2017. Development of Scala Native began in 2015 with a goal of being faster than just-in-time compilation for the JVM by eliminating the initial runtime compilation of code and also providing the ability to call native routines directly.
A reference Scala compiler targeting the .NET Framework and its Common Language Runtime was released in June 2004, but was officially dropped in 2012.Expunged the .net backend. by paulp · Pull Request #1718 · scala/scala · GitHub. Github.com (2012-12-05). Retrieved on 2013-11-02.
Examples
"Hello World" example The Hello World program written in Scala 3 has this form: @main def main() = println("Hello, World!")
Unlike the stand-alone Hello World application for Java, there is no class declaration and nothing is declared to be static.
When the program is stored in file HelloWorld.scala, the user compiles it with the command:
$ scalac HelloWorld.scala and runs it with $ scala HelloWorld
This is analogous to the process for compiling and running Java code. Indeed, Scala's compiling and executing model is identical to that of Java, making it compatible with Java build tools such as Apache Ant.
A shorter version of the "Hello World" Scala program is:
println("Hello, World!")
Scala includes an interactive shell and scripting support. Saved in a file named HelloWorld2.scala, this can be run as a script using the command:
$ scala HelloWorld2.scala Commands can also be entered directly into the Scala interpreter, using the option : $ scala -e 'println("Hello, World!")'
Expressions can be entered interactively in the REPL:
$ scala Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131). Type in expressions for evaluation. Or try :help.
scala> List(1, 2, 3).map(x => x * x) res0: List[Int] = List(1, 4, 9)
scala>
Basic example The following example shows the differences between Java and Scala syntax. The function mathFunction takes an integer, squares it, and then adds the cube root of that number to the natural log of that number, returning the result (i.e., ):
// Java: int mathFunction(int num) { int numSquare = num*num; return (int) (Math.cbrt(numSquare) + Math.log(numSquare)); } // Scala: Direct conversion from Java
// no import needed; scala.math // already imported as `math` def mathFunction(num: Int): Int = var numSquare: Int = num*num return (math.cbrt(numSquare) + math.log(numSquare)). asInstanceOf[Int] // Scala: More idiomatic // Uses type inference, omits `return` statement, // uses `toInt` method, declares numSquare immutable
import math.* def mathFunction(num: Int) = val numSquare = num*num (cbrt(numSquare) + log(numSquare)).toInt
Some syntactic differences in this code are:
Scala does not require semicolons (;) to end statements. Value types are capitalized (sentence case): Int, Double, Boolean instead of int, double, boolean. Parameter and return types follow, as in Pascal, rather than precede as in C. Methods must be preceded by def. Local or class variables must be preceded by val (indicates an immutable variable) or var (indicates a mutable variable). The return operator is unnecessary in a function (although allowed); the value of the last executed statement or expression is normally the function's value. Instead of the Java cast operator (Type) foo, Scala uses foo.asInstanceOf[Type], or a specialized function such as toDouble or toInt. Function or method foo() can also be called as just foo; method thread.send(signo) can also be called as just thread send signo; and method foo.toString() can also be called as just foo toString.
These syntactic relaxations are designed to allow support for domain-specific languages.
Some other basic syntactic differences:
Array references are written like function calls, e.g. array(i) rather than array[i]. (Internally in Scala, the former expands into array.apply(i) which returns the reference) Generic types are written as e.g. List[String] rather than Java's List<String>. Instead of the pseudo-type void, Scala has the actual singleton class Unit (see below).
Example with classes The following example contrasts the definition of classes in Java and Scala.
// Java: public class Point { private double x, y; public Point(double x, double y) { this.x = x; this.y = y;
public Point(double x, double y, boolean addToGrid) { this(x, y);
if (addToGrid) grid.addPoint(this);
public Point() { this(0.0, 0.0);
private void addPoint(Point p) { x += p.x; y += p.y;
public double getX() { return x;
public double getY() { return y;
double distanceToPoint(Point other) { return distanceBetweenPoints(x, y, other.x, other.y);
private static Point grid = new Point();
static double distanceBetweenPoints( double x1, double y1, double x2, double y2) { return Math.hypot(x1 - x2, y1 - y2);
} // Scala class Point( var x: Double, var y: Double, addToGrid: Boolean = false ): import Point.*
def += (p: Point) = x += p.x y += p.y
if (addToGrid) grid += this
def this() = this(0.0, 0.0)
def distanceToPoint(other: Point) = distanceBetweenPoints(x, y, other.x, other.y)
end Point
object Point: private val grid = new Point()
def distanceBetweenPoints(x1: Double, y1: Double, x2: Double, y2: Double) = math.hypot(x1 - x2, y1 - y2)
The code above shows some of the conceptual differences between Java and Scala's handling of classes:
Scala has no static variables or methods. Instead, it has singleton objects, which are essentially classes with only one instance. Singleton objects are declared using object instead of class. It is common to place static variables and methods in a singleton object with the same name as the class name, which is then known as a companion object. (The underlying class for the singleton object has a $ appended. Hence, for class Foo with companion object object Foo, under the hood there's a class Foo$ containing the companion object's code, and one object of this class is created, using the singleton pattern.) In place of constructor parameters, Scala has class parameters, which are placed on the class, similar to parameters to a function. When declared with a val or var modifier, fields are also defined with the same name, and automatically initialized from the class parameters. (Under the hood, external access to public fields always goes through accessor (getter) and mutator (setter) methods, which are automatically created. The accessor function has the same name as the field, which is why it's unnecessary in the above example to explicitly declare accessor methods.) Alternative constructors can also be declared, as in Java. Code that would go into the default constructor (other than initializing the member variables) goes directly at class level. In Scala it is possible to define operators by using symbols as method names. In place of addPoint, the Scala example defines +=, which is then invoked with infix notation as grid += this. Default visibility in Scala is public.
Features (with reference to Java) Scala has the same compiling model as Java and C#, namely separate compiling and dynamic class loading, so that Scala code can call Java libraries.
Scala's operational characteristics are the same as Java's. The Scala compiler generates byte code that is nearly identical to that generated by the Java compiler. In fact, Scala code can be decompiled to readable Java code, with the exception of certain constructor operations. To the Java virtual machine (JVM), Scala code and Java code are indistinguishable. The only difference is one extra runtime library, scala-library.jar.
Scala adds a large number of features compared with Java, and has some fundamental differences in its underlying model of expressions and types, which make the language theoretically cleaner and eliminate several corner cases in Java. From the Scala perspective, this is practically important because several added features in Scala are also available in C#.
Syntactic flexibility As mentioned above, Scala has a good deal of syntactic flexibility, compared with Java. The following are some examples: