1. Hello Scala

Pankaj Patil
7 min readJan 13, 2022

1. Why Scala?

Why should you spend time and effort in learning Scala?
Why is it becoming so popular?
What are the advantages of adopting it? Let’s discuss its main selling points.

THE JVM
The JVM is the standard platform for running Scala programs. Sun Microsystems introduced it in 1994 — more than 25 years ago. Since then, the industry has been extensively relying on it. The Java community has also been extremely active, and it has produced an impressive amount of libraries. Thanks to its integration with Java, you can use all these libraries in your Scala programs; this is also true for any Java legacy code you or your company may have.

A HYBRID LANGUAGE
Scala manages to combine two programming techniques that are considerably different: the object-oriented style with the functional one. When executing code on the JVM, the object-oriented approach can be more performant but prone to errors. When using mutable state, your program will re-allocate its memory: every time a change occurs, it will change the data in place. However, sharing state can cause your application to suffer from data inconsistency issues due to multiple processes accessing and modifying the same portion of data.
A functional approach can be more readable and reusable but not as performant. Thanks to immutable data and structures, you can guarantee your program’s correctness when dealing with concurrency: your data never changes, so it is safe to share it. Your code will also be easier to understand and reuse because all its components will be independent of external factors outside its control. But recreating data rather than updating could be a memory-expensive operation, even though it has massively improved thanks to numerous optimizations and efficient garbage collection strategies in recent years.
In Scala, you do not have to stick to a particular style, but you can take advantage of one or the other paradigm depending on the specific task you are solving. Look at below figure for a comparison of how the two approaches tackle different programming tasks.

Comparison of the object-oriented and functional programming styles and how they handle different programming tasks

CONCISE SYNTAX
Scala’s programming style is relatively concise, particularly when compared to other languages like Java. Having a compact syntax can increase both the productivity and readability of your program. At the beginning of your journey with Scala, you may find it quite overwhelming.

FLEXIBILITY
Scala is extremely flexible: you can achieve the same goal in more than one way, making the language extremely fun and exciting to use. The opportunity to choose between different programming paradigms allows you to gradually shift your mindset from one approach to another without committing to a specific style since day one. For example, you can dip your toe in the functional programming world without any long-term commitment

CONCURRENCY
Thanks to its use of immutable data structures and expressive type system, dealing with concurrency is less prone to errors than in other languages. As a result, Scala programs tend to utilize resources more efficiently, and they usually perform better under pressure.

BIG DATA AND SPARK
Thanks to Scala’s features and optimizations at its compile level, the community has developed new performant tools for big data processing. Apache Spark is the most popular of these tools. Thanks to Scala’s lazy evaluation, Spark can perform optimizations at compile time that have huge impacts on its runtime performance:
“Run programs up to 100× faster than Hadoop MapReduce in memory, or 10× faster on disk.”
Source: https://spark.apache.org

2. Scala and the JVM

Scala takes its name from the word scalable. Martin Odersky and his team designed it in 2004 with the intent of creating a language for the Java Virtual Machine (JVM) that can easily handle high volumes of requests.

To understand the execution of a Scala program, let’s compare it with the typical execution flow of a Java program; below figure provides a visual representation of the two processes.
The JVM is a machine to perform tasks by executing a well-defined set of operations, called bytecode. A JVM language aims to translate its code into executable JVM bytecode, usually formed by multiple files with extension *.class.
When coding in Java, you save your source files with extension *.java and use the compiler javac to produce a jar file containing the generated bytecode. When writing Scala code, your source files have extension *.scala. The Scala compiler called scalac is in charge of translating the code into bytecode.
You can seamlessly add Java sources and depend on Java libraries in your Scala codebase. The Scala compiler fully integrates with the bytecode that the Java compiler produces, making the integration between the two languages straightforward.

Comparison between the execution of a Java program with a Scala one. The Java source code has extension *.java, and javac translates it into bytecode files with extension *.class. You save Scala code in files with extension *.scala, and scalac is the compiler responsible for converting them into bytecode. The JVM is now ready to run the bytecode, usually conveniently grouped in a *.jar file.

Comparison between the execution of a Java program with a Scala one. The Java source code has extension *.java, and javac translates it into bytecode files with extension *.class. You save Scala code in files with extension *.scala, and scalac is the compiler responsible for converting them into bytecode. The JVM is now ready to run the bytecode, usually conveniently grouped in a *.jar file.

Scala as a multiplatform language
Odersky and his team designed Scala for the JVM. A few open source projects (some more experimental than others) are trying to develop new compilers so that you can use the language in other platforms too.

scala-js is a well-established open source project that allows you to compile Scala code to JavaScript. It is a framework to build applications that run in your browser. It enables you to share code between the front and the backend of your application. Visit https://www.scala-js.org for more information.

scala-native (see http://www.scala-native.org) is another project targeting Scala for the Low-Level Virtual Machine (LLVM) compiler. Its development is in progress but looks promising; running Scala programs on the LLVM would optimize their speed and memory.

Finally, if you are looking for an alternative (more fun?) way of writing CSS code, look at scalacss (see https://github.com/japgolly/scalacss) for a “super type-safe CSS” in Scala.

3. Scala’s key features

Since 2004, Scala has evolved a lot, but its fundamental features have not changed. In this section, you’ll glance at a few fragments of Scala code.

SCALA IS OBJECT-ORIENTED
An object-oriented programming language has a structure based on classes and subclasses. They have well-defined behaviors, and they exchange information through methods.

MyClass Example

Singleton instances are instances that you should initialize at most once. Scala offers a dedicated syntax for them: you usually refer to them as objects. Do not be confused by the term object, as it can refer to an instance rather than a singleton in other languages.

MySingleton example

Exceptions are another typical feature of an object-oriented language. They represent code anomalies: you can throw and catch them to control your program’s execution flow. In Scala, exceptions are similar to those in languages such as Java, Python, and C++.

Example of throwing and catching exceptions

Mutable data structures and assignments are also part of the language. The language’s design discourages their use, but you can still take advantage of them when needed.

SCALA IS FUNCTIONAL
Functional Programming languages base their entire structure on functions. Functions play a big part in Scala as its first-class citizens. In scala you’ll discover functional purity and its several advantages and how you can use functions as parameters or return values: you refer to them as higher order functions. They allow you to create powerful abstractions that simplify and remove code duplication in a way that is usually not so easily achievable in an object-oriented approach.

Example of Higher Order Function

SCALA HAS A ROBUST TYPE SYSTEM
Scala is a statically typed language. Types define the acceptable range of values for your computation. Thanks to them, the compiler can check at compile time that your code doesn’t violate any constraints, which makes your code more reliable and less prone to errors at runtime. Scala has a type inference system, so you do not have to specify the intended type for every expression of your program, making your code less verbose.
Languages that do not have a type inference system require you to provide types explicitly and tend to be more verbose. Scala’s type system is also quite flexible: you can reuse existing or create custom types to ensure business requirements at the type level.

SCALA’S INTEGRATION WITH JAVA
Scala is a JVM language that you can easily integrate Java code into your Scala programs. Most IDEs that provide Scala support, such as IntelliJ IDEA, can even rewrite Java code into Scala code automatically.

Java to Scala code example

Alternatively, you can also add files with extension *.java directly into your Scala project; Scalac will recognize them as Java and compile them accordingly.

--

--

Pankaj Patil

Software Engineer | Blogger | Reader | Writer| Political and Social Activist