Skip to main content

Beginner’s Guide to Scala

Learn the basics in Scala including syntax, how to set up an environment, and various common data types.
Aug 2019  · 6 min read


scala logo

Scala was designed by Martin Odersky, a professor at École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland and a German computer scientist. Martin started working on creating Scala in 2001, and it was first released in 2004.

Scala stands for Scalable Language. It is a general-purpose, object-oriented language which has a few commonalities with the Java programming language. It provides support for functional programming. Many of Scala's design decisions were aimed to address the drawbacks of the Java programming language.

A few features that Scala offers are:

  • Scala's source code can be compiled to Java bytecode and hence, are executable on a Java Virtual Machine (JVM).

  • It provides language interoperability with Java so that libraries written in either language can be referenced in Scala or Java codebase.

  • Like Java, Scala also uses a curly-brace syntax.

  • It has many features of functional programming languages like Scheme, Standard Machine Learning and Haskell, including currying, type inference, immutability, lazy evaluation, and pattern matching.

  • Support for Web application using JavaScript.

  • Unlike Java, Scala also supports operator overloading, optional parameters, named parameters, and raw strings.

One Interesting fact about Scala is that it is widely used by companies like Apple, Twitter, Walmart, and Google because of its scalability and the capability of being used in backend operations.

Setting up Scala in Jupyter Notebook

Jupyter notebook is the most widely used tools in computer science, especially in the data science domain. It also has support for Scala development with the help of spylon-kernel.

Note: For those of you, who are new to jupyter notebook, please feel free to check out this comprehensive tutorial.

Setting up the Scala environment in jupyter notebook is not rocket science. So, let's quickly set it up in just a few steps.

  • First, you will install the spylon-kernel.

    pip install spylon-kernel (Python 2)
    pip3 install spylon-kernel (Python 3)

  • Next, you will create a kernel specification which will allow you to select the scala-kernel in jupyter notebook.

    python -m spylon_kernel install (Python 2)
    python3 -m spylon_kernel install (Python 3)

  • Then, as an important step, you will install pyspark and findspark since spylon-kernel also installs some spark components.

    pip install pyspark findspark (Python 2)
    pip3 install pyspark findspark (Python 3)

  • Finally, start your jupyter notebook and choose kernel as spylon-kernel and you are good to go!

jupyter notebook example

  • Let's run a small piece of code to test whether the Scala kernel was correctly configured or not.
val x = 2
val y = 3
x: Int = 2
y: Int = 3
res1: Int = 6

Great! So as you can observe from the above output, you were successful in configuring the Scala environment in jupyter notebook.

Basic Syntax

Let's start by printing Hello, World! in Scala.

print("Hello, World!")
Hello, World!

As you can see, it was so simple, just a print command followed by the input as an argument.

Now, let's take a look at few Syntax related details in Scala:

  • Scala is case-sensitive, which means identifier DataCamp and dataCamp would have a different meaning in Scala.

  • In Scala, all class names first letter should be in Upper Case. If many words are combined to form a name of the class, each separate word's first letter should be in Upper Case. For example, class MyScalaDataCampTutorial.

  • The method names in Scala work slightly differently as compared to class names mainly to distinguish the method names from class names. The method names should start with a Lower Case letter (Source). If multiple words are combined to form the name of the method, then each inner word's first letter should be in Upper Case. For example, def firstDatacampScalaTutorial()

  • In Scala, the name of the program file should exactly match the object name. When saving the file, you need to save it using the object name and append .scala to the end of the name. For example, Let's say ScalaTutorial is the object name. Then the file should be saved as ScalaTutorial.scala.

    Note: If the file name and the object name does not match, then your program will not even compile.

  • Finally, like most programming languages function, Scala program processing also starts from the main() method, which is a crucial part of every Scala Program.

Below figure shows the reserved words in Scala which cannot be used as constants or variables or as any other identifiers.

reserved words in scala

Data Types

Data types in Scala are almost identical to many other programming languages like C, C++, Java, Python, etc. with similar memory footprint and precision.

Finally, let's take a look at a few of the data types that exist in Scala:

  • Int: A 32-bit signed (can take both positive as well as negative values) value has a range from $2^{-31}$ to $2^{31} - 1$

  • Byte: An 8-bit signed value has a range from $2^{-7}$ to $2^{7} - 1$

  • Short: A 16-bit signed value has a range from $2^{-15}$ to $2^{15} - 1$

  • Long: A 64-bit signed value has a range from $2^{-63}$ to $2^{63} - 1$

  • Float: Both 32-bit (single-precision float) and 64-bit (double precision float)

  • Char: A 16-bit unsigned (can only take positive values) Unicode character.

A few of the other data types are String, Boolean, Null.


Congratulations on finishing this tutorial.

It is a good starting point for beginners who are interested in learning Scala.

To learn more, check out DataCamp's Scala tutorials:

Basics of Functions and Methods in Scala

IF ELSE in Scala

Operators in Scala

Lists in Scala

There is a plethora of information related to Scala that remains unraveled like if/else, for loops, various operators in Scala, etc., which will be covered in the future tutorials, so stay tuned!


Please feel free to ask any questions related to this tutorial in the comments section below.


Learn more about Data Science and Scala


Introduction to Scala

3 hr
Begin your journey with Scala, a popular language for scalable applications and data engineering infrastructure.
See DetailsRight Arrow
Start Course
See MoreRight Arrow


Lists in Scala

Learn what lists are and how they can be leveraged in the Scala Programming Language.
Aditya Sharma's photo

Aditya Sharma

6 min


Scala Classes and Objects

In this tutorial, you will learn about the fundamental notions of object-oriented programming in Scala: Classes and Objects.
Aditya Sharma's photo

Aditya Sharma

8 min


Variables in Scala

Learn about Scala's variables, rules for defining variables, different types of variables with multiple declaration and assignments along with the scope of a variable.

Olivia Smith

10 min


Operators in Scala

Learn about the different operators used in the Scala programming language.
Aditya Sharma's photo

Aditya Sharma

6 min


Basics of Functions and Methods in Scala

Learn about the basics of methods, named arguments, default parameter values, variable arguments in addition to different kinds of functions.

Olivia Smith

10 min


Scala Traits

Learn about the use of traits in Scala with the help of syntactic examples.
Aditya Sharma's photo

Aditya Sharma

6 min

See MoreSee More