Ever since its release, Scala has been gaining popularity, not just by programmers but by some of the most well-known companies. In an ever-growing world, Scala provides the tools to scale programs based on the required needs, making it a very practical and sought-after language.
If you’re looking for an alternative to Java, or just want to try out a new language, here’s how you can get your start with Scala.
Here’s what this post will cover:
- What is Scala?
- Why the resurgence of Scala? What makes it unique?
- A featurette: Scala features
- Scala vs Java
- Why use Scala in 2020
Scala, i.e. Scalable Language, is a general purpose, high-level language that is statically typed. Scala, created by Martin Odersky, was designed to be a “better version” of Java, combining both functional and object-oriented programming into one concise package, making it an extremely logical and powerful language.
Odersky’s primary influence when developing Scala was the Java programming language, so much so that Scala is written to run in Java Virtual Machine. Scala programs compile to JVM bytecodes, and their runtime performance is mostly on par with Java programs, and in some cases Scala code compiles quicker due to its advanced compiler, scalac.
Scala allows you to call Java methods, access Java fields, inherit from Java classes and implement Java interfaces in your code without the added requirement of any special syntax.
While the parallels between Java and Scala are evident, Scala differs in many ways with its code being much more concise, clean, and with its own benefits which are discussed throughout this post.
Scala isn’t a new programming language as its been around since 2004. So why are we seeing a rising popularity in Scala? Listed below are a few reasons:
Scala supports both functional programming and object-oriented programming making it a very scalable language. There are other languages that provide both functional and object-oriented programming, but none are able to fuse them together into one uniform language the way Scala does.
Scala is a pure object-oriented language, but it’s also fully functional, giving developers the best of both worlds. As Leda designer Timothy Budd puts it: “The idea of a multi-paradigm language is to provide a framework in which programmers can work in a variety of styles, freely intermixing constructs from different paradigms.” This equips the programmer with a larger number of tools to solve a wider range of problems.
What’s great about this approach is that you’re able to think about problems in different ways, as opposed to just FP and OOP. One such example is the Cake Pattern, “a technique used to modularize your code into layers (much like a cake) using mixins or traits, an artifact very much related to OO style of programming. Within the component you can use pure functions to build your domain logic.”
Scala is considered to be an expressive language, in that it’s easy to understand for both the compiler and the developer reading it. There a few contributing factors that make Scala an expressive language:
- Scala is statically typed
- Works with immutable values only
- Makes use of type inferencing
- Clean/clear syntax
- Take a look at this example, both accomplish the same thing:
credit via stackoverflow
Scala runs on the JVM, which also means that developers will still have access to Java libraries they’ve used for years. With that said, if Java developers were looking to make the switch to Scala, they’ll have an easier time leveraging their years of experience programming in Java.
The LinkedIn website was launched in 2003 and is now the largest professional networking site in the world with more than 65 million members, representing 200 countries and executives from every Fortune 500 company. It switched to Scala in 2009.
Twitter is an extremely popular live message service with over 70 million users. As Twitter has scaled up its services, it has moved its main message queue from Ruby on Rails to Scala for better performance in 2009.
Sony is an industry leader in the world of computer graphics. They have released five open-sourced projects, one which is the Scala Migrations, a library that manages updates, rollbacks, and changes to database schemas. The library is written in Scala and provides a database abstraction layer that allows migrations to target any supported database vendor.
Type inference is Scala’s ability to infer types without them being explicitly mentioned. The language is smart enough to deduce data types based on the context in which the object has been written.
Immutability is the quality of being unchangeable. In Scala, variables are immutable by default; once assigned a value they cannot be modified by you or any other programmer. This might not be apparent now, but default immutability is one of Scala’s strongest features. It is primarily used for concurrency control which manages concurrent (simultaneous) operations so that they do not conflict with each other. You can also create immutable variables by explicitly mentioning so in your code.
Scala only evaluates an expression when required. This increases performance by reducing compile time.
In Scala, case classes are regular classes with the added feature of being immutable. This makes them great for modeling immutable data.
Case classes are useful for pattern matching, which is used for checking a value against a pattern and deconstructing it into its constituent parts.
Scala’s string interpolation methods allow you to embed variables directly inside a string literal allowing the creation of strings through data.
Higher-order functions are functions which take other functions as arguments. They can also return functions along with simple data types.
Traits are a collection of abstract and non-abstract methods. They are similar to Java’s interface. Simply put, while classes and objects can only inherit from a single class, they can inherit from multiple traits.
Scala provides an extensive collection of classes and traits which are used for collecting data. They are divided into two broad categories of mutable and immutable collections and are all contained in Scala’s package Scala.collection.
While using Java, developers typically have to write more lines of code, while Scala enables developers to accomplish the same tasks without writing so much code, making it more expressive. Further, it helps developers to make code more organized, readable and reusable. So, some developers opt for Scala to develop a concise, readable and maintainable code base.
To help manage the quality of your code, Scala makes use of type inferencing, eliminating much of the frustrating and overly tedious syntax that Java developers face. Often, code written in Scala is more concise, but it also happens to be a little more complex, so beginners usually prefer working with Java.
Yes, Java also has functional programming capabilities, but was added later as a feature in Java 8. Scala on the other hand was designed from the beginning as a functional programming language and an object-oriented language. Unlike Java, it has much better and advanced functional programming features like monads, immutability and concurrency.
While using Java 8, developers are required to use the thread-based concurrency model, dividing the program into several concurrently running tasks at the time of execution. But Scala uses the more efficient actor model, allowing developers to define each object as an actor with its own behavior and mailbox, ultimately simplifying thread communication and control.
Scala’s popularity is growing considerably, and as more and more companies adopt the language, we’ll only see a greater demand. According to Indeed, Scala is number 12 on the most demanded languages list.
Those that know Java can make the switch to Scala fairly easily due to the similar properties that they share. Java developers may even find that they enjoy working more with Scala because they may be able to accomplish more in a shorter amount of time; in the end Scala was established to make you a more productive developer.
If you’re searching for an alternative to Java or just want to try something new, than Scala is going to be a great option. It is regarded as the alternative to Java, and in terms of learning a new language, Scala is extremely practical and serves many use cases which is why it has surged into the job market. Learn Scala from scratch today.