top of page

Scala Programming Language - An Introduction

  • Writer: Stefano Giannini
    Stefano Giannini
  • Aug 15, 2022
  • 3 min read

1. What is Scala?

Scala is a strong statically typed general-purpose programming language which combines object-oriented and functional programming.

The name Scala comes from the word scalable, and true to that name, the Scala language is used to power busy websites and analyze huge data sets.


The main features are:

  • It’s a high-level programming language

  • It has a concise, readable syntax

  • It’s statically-typed (but feels dynamic)

  • It has an expressive type system

  • It’s a functional programming (FP) language

  • It’s an object-oriented programming (OOP) language

  • It supports the fusion of FP and OOP

  • Contextual abstractions provide a clear way to implement term inference

  • It runs on the JVM (and in the browser)

  • It interacts seamlessly with Java code

  • It’s used for server-side applications (including microservices), big data applications, and can also be used in the browser with Scala.js

  • It uses lazy evaluation



2. Why should I learn it?

If you are a data scientist and using only Python/ R is not enough to analyze large amounts of data, you should give Scala (or Julia) a try.

Moreover modern web applications (both back-end and front-end) can take advantage of the scalable features of Scala. Also the seamless integration with Java offers a vast amount of libraries available, with the advantage of less "boiler-plate" code with respect to Java.



3. How to install Scala

If you would like to experiment with Scala language without installing anything on your PC, you should try Scastie on your browser.

On Windows simply download and run the installer.

On other platforms fo the official guide.



4. Hello world!

4.1. Code:

@main def hello() = println("Hello, World!")

"hello" is a method of main thanks to the "@main" annotation.

As in other programming languages println method prints the argument string (in this case "Hello, World!") to the standard output (STDOUT).


The code has to be saved into a text file with ".scala" extension. In this case we can call it "hello.scala".


4.2. Compilation (on your terminal, not in the browser version):

$ scalac hello.scala

4.3. Execution:

$ scala hello
Hello, World!

If you want to try an interactive "Hello, World!" version where an user input is asked, follow this simple tutorial.



5. Variables and Data Types


5.1. Two variables types

val: an immutable variable, it is the recommended way to declare a variable unless you need it to be mutable.

var: a mutable variable, it should be used on when its contents changes over time.


// immutable
val a = 0

// mutable
var b = 1

// reassignment to val error, failed compilation
val msg = "Omae Wa"
msg = "Mou Shindeiru"

// no error, successful compilation
var msg = "Omae Wa"
msg = "Mou Shindeiru"

5.2. Variable types declaration

You can declare variables both explicitly and implicitly.

val x: Int = 1    // explicit
val x = 1         // implicit; the compiler infers the type
val s = "a string"
val nums = List(1,2,3)

The implicit form is a great way to keep the code concise (one of the main goal of Scala). In general it is recommended to use the implicit declaration for simple assignment like the ones shown previously.


5.3 Built-in data types

Since everything is an object in Scala, every data types a vast amount of class methods.

Explicit:

val b: Byte = 1
val i: Int = 2
val l: Long = 3
val s: Short = 4
val d: Double = 5.0
val f: Float = 6.0

Implicit:

val i = 111    // defaults to Int
val j= 1.0     // defaults to Double

// L, D, F suffix
val x = 1_000L   // val x: Long = 1000
val y = 2.3D     // val y: Double = 2.3
val z = 3.5F     // val z: Float = 3.5

Large numbers:

var a = BigInt(1_234_567_890_987_654_321L)
var b = BigDecimal(123_456.789)

String and Chars:

val name = "Steffi"    // String
val c = 'a'            // Char

// multiline strings (as in Python)
val quote = """The essence of Scala:
               Fusion of functional and object-oriented
               programming in a typed setting."""
               
// string interpolation
val firstName = "St."
val mi = 'J'
val lastName = "Kilo"

println(s"Name: $firstName $mi $lastName")   // "Name: St. J Kilo"

println(s"2 + 2 = ${2 + 2}")   // prints "2 + 2 = 4"val x = -1
println(s"x.abs = ${x.abs}")   // prints "x.abs = 1"

For further details, see the official guide


 
 
 

Comments


bottom of page