Introduction to Scala Programming Language

Introduction to Scala Programming Language

Scala, short for Scalable Language, is a general purpose programming language that is both object oriented and functional. It is designed to help programmers write code that is simple, sophisticated, and type safe, while staying productive.

Scala is a relatively young language, first released in 2004, but it has built a strong and active community, largely because it is considered one of the more approachable languages once you already know some Java. Scala draws influence from Java, Haskell, and Lisp, among others, and Scala source files use the .scala or .sc extension. You can build web applications, enterprise systems, mobile apps, and desktop software with it.

A Basic Scala Example

Below is a simple Scala program, along with the output it produces once compiled and run.

Input

Introduction to Scala Programming Language

Output

main output

Prerequisites to Learn Scala

Scala has a fairly light set of prerequisites. If you already know some C or C plus plus, you can get started without much trouble. Since Scala is built on top of Java, and its basic program structure closely follows Java’s, a working knowledge of Java syntax and object oriented concepts will make learning Scala noticeably easier.

Scala was created by Martin Odersky, who began working on it in 2001 at the Ecole Polytechnique Federale de Lausanne, and it was officially released on January 20, 2004. It is not an extension of Java, but it is fully interoperable with it. When you compile a Scala file, it translates into Java bytecode and runs on the JVM, the same Java Virtual Machine that runs Java itself.

Scala was designed to be both object oriented and functional from the ground up. It is object oriented in the sense that every value is an object, and functional in the sense that every function is itself a value. Its name, short for scalable language, reflects the intent that it should grow comfortably as a project’s demands grow.

Immutability: Val and Var

One of the first things that feels different coming from Java or Python is how Scala treats variables. When you declare a value with val, it cannot be reassigned once set, it is immutable. When you declare one with var, it can be changed later, the same as a normal variable in most other languages. Scala code leans toward val by default, which is part of why Scala programs tend to have fewer of the bugs that come from a value changing somewhere unexpected in a large codebase.

Why Scala Is Popular Among Programmers

  • Easy to start, since it is a high level language close to Java, C, and C plus plus, making it particularly approachable for existing Java programmers.
  • Draws on the best of several languages, combining ideas from C, C plus plus, and Java into something more scalable and productive than any one of them alone.
  • Close integration with Java, since the Scala compiler can interpret Java classes directly and make use of existing Java libraries, frameworks, and tools.
  • Works for both web and desktop development, compiling to JavaScript for the web and to JVM bytecode for desktop and backend applications.
  • Used by major companies, including Apple, Twitter, and Walmart, many of whom moved backend code to Scala specifically for its scalability.

Where You Can Use Scala

  • Web applications
  • Utilities and libraries
  • Data streaming with Akka
  • Parallel batch processing
  • Concurrency and distributed applications
  • Data analysis with Spark
  • AWS Lambda expressions
  • Ad hoc scripting in the REPL

Uses Scala

Scala lets you build any of these application types with less code than an equivalent Java project, and frameworks such as Play make it faster to get a web application running.

Scala Compared to Java

Java Scala
More verbose syntax, more code to express the same logic More concise syntax, often half the lines for the same logic
Statically typed, with more boilerplate around types Statically typed, with strong type inference that reduces boilerplate
Purely object oriented Object oriented and functional at the same time
Runs on the JVM Also runs on the JVM, and compiles to Java bytecode

Scala and Java share a lot of common ground, both compile to JVM bytecode, but Scala’s syntax is generally more concise, and code written in Java can often be expressed in roughly half the lines once rewritten in Scala.

Scala Compared to Python

Python Scala
Dynamically typed, types are decided at run time Statically typed, types are checked at compile time
Generally easier to pick up for a first time programmer A steeper learning curve, especially around functional programming concepts
Widely used for data science, with a huge library ecosystem Widely used for data engineering, tightly integrated with Apache Spark
Larger community overall A smaller but strong community, especially in the Spark ecosystem

Python and Scala solve different problems well. Python’s simple syntax and huge library ecosystem make it the easier starting point and the dominant choice for data science and machine learning. Scala’s static typing and tight integration with Apache Spark, which is itself written largely in Scala, make it a strong choice once you are working with large scale data processing, even though it takes longer to feel fully comfortable in the language.

Features of Scala

  • Object oriented, every variable and value is implicitly treated as an object.
  • Extensible, capable of supporting new language constructs through libraries and APIs, without needing a separate domain specific language.
  • Statically typed, binding a data type to a variable for its entire scope.
  • Functional, with lightweight syntax for defining functions, support for higher order functions, and the ability to nest functions inside each other.

Interoperable, compiling through the Scala compiler into Java bytecode that runs on the JVM.

Extensible Programming Language

Frameworks Built on Scala

  • Akka, a toolkit for building highly concurrent, distributed, and fault tolerant applications on the JVM, with bindings for both Scala and Java.
  • Spark, designed to handle and process big data, and built specifically around the Scala language.
  • Play, built for web applications, using Scala to reach strong, class leading performance.
  • Scalding, a domain specific language built on Scala that integrates with Cascading, closer in spirit to the original MapReduce model than plain Java is.
  • Neo4j, a graph database supported by Scala, with domain specific functionality and graph algorithms.

Frameworks in scala

Scope of Scala in the Future

Scala has grown steadily since its release and remains one of the more in demand languages in data engineering, largely on the strength of its role inside Apache Spark.

Scope with Scala in future

The chart below breaks down permanent versus contract based jobs available to people with Scala experience.

chart jobs permanent jobs or Contract-based

Common Misconceptions to Watch For

  • Assuming Scala only exists for Apache Spark. Spark is Scala’s most visible use case, but Scala is a general purpose language used for web development, concurrent systems, and more.
  • Expecting Scala to be as quick to pick up as Python. Its functional programming concepts and type system take longer to feel natural, even for experienced programmers, so budget more time for the early learning curve than you might for Python.
  • Overusing var out of habit. Coming from a language without a strong immutable default, it is easy to reach for var everywhere. Defaulting to val, and only using var when a value genuinely needs to change, is closer to how idiomatic Scala code is written.

Watch It in Action

The video below, embedded on the original page, walks through these Scala concepts.

That covers the basics of Scala, how it compares to Java and Python, and where it fits in a data engineering career. To go further, explore Prwatech’s Apache Spark training program, which includes placement assistance.

Popular Tags:

Features of Scala programming Intro to Scala Introduction to Scala Programming Language Learn scala programming Prerequisites for Scala Programming Scala Frameworks scala programming Scala Programming Language Tutorial Scala uses What is Scala programming language