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

The Complete Docker Certification (DCA) Guide for 2024

Unlock your potential in Docker and data science with our comprehensive guide. Explore Docker certifications, learning paths, and practical tips.
Matt Crabtree's photo

Matt Crabtree

8 min

Mastering API Design: Essential Strategies for Developing High-Performance APIs

Discover the art of API design in our comprehensive guide. Learn how to create APIs like Google Maps API with best practices in defining methods, data formats, and integrating security features.

Javeria Rahim

11 min

Data Science in Finance: Unlocking New Potentials in Financial Markets

Discover the role of data science in finance, shaping tomorrow's financial strategies. Gain insights into advanced analytics and investment trends.
 Shawn Plummer's photo

Shawn Plummer

9 min

5 Common Data Science Challenges and Effective Solutions

Emerging technologies are changing the data science world, bringing new data science challenges to businesses. Here are 5 data science challenges and solutions.
DataCamp Team's photo

DataCamp Team

8 min

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

Introduction to DynamoDB: Mastering NoSQL Database with Node.js | A Beginner's Tutorial

Learn to master DynamoDB with Node.js in this beginner's guide. Explore table creation, CRUD operations, and scalability in AWS's NoSQL database.
Gary Alway's photo

Gary Alway

11 min

See MoreSee More