All Courses
What is Scala

What is Scala?

What is Scala?

Scala is a high-level programming language that is meant to both object-oriented and functional. Scala is designed to be a powerful, developer-friendly and a fast programming language.  The latest version of Scala at the time of writing this blog is 2.12.8. The word ‘Scala’ is the blending of two words: Scalable and Language. This has been designed by Martin Odersky in 2004. It is incorporated with all the features which can grow or scale with the needs of its users.

Scala is built in a way that it grows according to the demand of its users. Scala was intended to run on java virtual machine so it can solve the problems of java while operating with Java.  Let us see why do we need Scala and what makes Scala different from other languages especially Java.  Scala is becoming popular day by day and there is a  rapidly increasing demand for Scala. This blog post is intended for absolute beginners who want to start their journey with Scala.

Why Scala?

Among the pool of programming languages, each one has its own features and benefits. On the same note, here are some notable properties of Scala which makes it stand as the Scalable Language.

Features of Scala

Object-oriented and Functional programming:

Scala is both object-oriented and functional programming language..Every value in Java is an object. Every function is a value and it is an object. It provides lightweight syntax for anonymous functions. It supports higher order functions, currying, nested functions thus making a very good functional programming language to work with.

Interoperable with JAVA:

SCALA uses JVM and Java libraries making it compatible with JAVA, which gives us a feasibility to incorporate SCALA into any application based on JAVA.

Immutability:

Immutable variables are the ones whose values cannot be changed or modified. In Scala, by default, every declared variable is treated immutable. Using immutable variables increases thread safety, easy to copy as here it is not required to copy complete object but we copy only a reference.

Static type system:

Like Java, the typing in Scala is static. that means a variable is both declared and defined so that the value of variable is known at the compile time itself. This improves the performance and makes things easy to find.This feature helps in fewer problems while executing the language. Hence, programmers will not end with problems that usually occur with dynamic typing. Scala also supports type interference, this makes its act as a dynamic language. In short words, Scala has the benefits of both static typing and dynamic type interference.

Executable on JVM:

Scala was built originally to run on the Java virtual machine. Scala is easily integrated with Java. A Java programmer can easily use Scala code in his/her system due to its interoperability.  Both Java and Scala are capable of executing in the other JDK. In other words, Java JDK can execute Scala and Java can be run on Scala JDK.

Framework support:

The frameworks Akka, Play and Spark are built upon Scala. Scala is growing every day and these are the frameworks which are very popular. Scala, due to its concurrent nature is widely used in Big Data applications along with Spark. Akka is another framework that uses Scala, which helps to build highly concurrent, resilient, distributed applications.

Java vs Scala:

As we all know, Java is an object-oriented language. Since Java 8, we are trying to make java to behave more like a functional language. Still, a lot of programmers find it hard to get along with the functional programming abilities of java. This is because we are all used to Java as an object-oriented language. How about a language that can easily integrate with Java, and still has both functional and object-oriented features. That is what Scala does.

Scala takes the best features of Java and still adds a few features which are not in java to address the real-time problems while working with Java.  Also, Scala makes the integration with Java almost seamless. It is very easy to work with both Java and Scala at the same time.  Any Java programmer can easily learn and work with Scala.  Still there a lot of features are available in Scala that makes it unique and different compared with Java. Unlike JAVA, where import statement should be used only in the beginning of the program, in Scala you can use import anywhere in the program. Scala is strongly typed – Providing data integrity.

Simple syntax:

Scala provides better syntax compared with Java. This makes Scala an easy language for learning.

Type interference:

There is no need to mention the type explicitly. Scala is dynamic enough to infer the type of arguments thus improving the efficiency of the programmer. Scala is equipped with the technique that can decide the data type on its own which means that a programmer need not specify the data type for any data or return type of a function.

Function return type is deduced from the last executed expression in that function which becomes a handy feature for the coders.

User friendly compilation:

Scala programs are compatible to be executed on web browsers as well using a compiler Scala.js which compiles the code to JavaScript.

Anonymous functions:

Anonymous functions are the functions without a name. These functions focus only on the functionality that is going to be implemented, rather than creating objects. Anonymous functions are meant to be passed as arguments in other higher-order functions. Scala provides capturing semantics to support anonymous functions.

Lazy evaluation:

Lazy evaluation is the concept of evaluating the value only when it is needed. This saves a lot of time and memory usage. With this lazy evaluation technique, Scala becomes a language which is faster and more efficient.

Higher-order functions:

Higher order functions are the functions that take other functions as arguments.

Currying:

If there are a set of arguments for a particular function, then the process of currying is converting that function to take a single argument which is the first argument. Then, this first argument returns a function that takes the second argument as input and returns another function, that takes the third argument as input and returns another function and so on. Scala supports this type of currying and helps to avoid passing the same variable again and again.

Pattern matching:

Pattern matching is the idea of finding a pattern in the given sequence of inputs. Java supports pattern matching using Regular expressions. In the case of Scala, case classes are available to support the pattern matching.

Parallel processing:

Scala has features such as Future, Promises which helps process the streaming data in parallel. This kind of ability makes Scala the best language to work with Big Data.

Getting started to write in Scala

SCALA code can be written, compiled and executed in following ways:

CLI mode:

  • Using Interactive Interpreter or
  • Writing the code in a VI editor and then compiling it.

GUI mode:

  • Using a feasible IDE.

What is an IDE?

An IDE – Integrated Development Environment is a tool that has all the needed software required to write and test software in a particular language.

An IDE is a tool packed with:

  • A code editor,
  • A compiler or interpreter
  • A debugger

Note that all are accessed through a single GUI.

IDE’s supported for SCALA are

  • ATOM
  • IntelliJ
  • Scala IDE for Eclipse
  • VS Code
  • Sublime Text

The “Hello World!!!” program

Let us begin our tour to Scala coding with our most familiar and friendly program, the “Hello World!!!” J.  The below piece of code, prints the statement “Hello, World!!!” on to the standard output device.
As discussed above the function HelloWorld is treated as an object.

Assuming the user to be using UNIX OS – using CLI, below are the steps to compile and get the needed output.

1. Download and Install the Scala package that suits the OS (for UNIX it is scala-2.12.8.tgz)

2. Setup the environment for compiling Scala by exporting the following variables:

export $SCALA_HOME=/usr/local/share/scala
export $PATH=$PATH:$SCALA_HOME/bin

Alternately these can be added to the ~/.bashrc file and sourced to be added to environment variables. Verify the same by trying to print the environment variables using “env”

3. There are 3 ways we can execute the code:

  • Using Interactive Mode:
  • When written as a scala program I,e. using <filename>.scala
  • As a script (for e.g. in bash shell)

4. Now, let us try to run this as a scala program:

  • Assume that the following source code to be present in a file name called HelloWorld.scala.
  • Next is to compile the code using scalac command whose output is a scala bytecode.

Scalac HelloWorld.scala

  • Then run the thus produced byte code using scala command

Scala HelloWorld

Program

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!!!")
  }
}

Important Points to remember

  • There is no need to end a scala statement with ;
  • Data types used are as Int, Double, Boolean i,e. beginning with a capital letter.
  • No need to define a return type to a function as it considers the return type of function to be the type of last expression executed in the function.
  • Instead of using a packagename* for import, Scala uses import packagename_

Thus, having read about executing a small piece of Scala code, let us take a look at the real time fields where SCALA is being used.

Real World applications of Scala

Designed and built with API’s and strong tools, Scala is already running over a wide range of applications in the real world.

  • As we have learnt above, with the use of Scala.js compiler, it is used in web applications.
  • Distributed applications – Spark which is the widely used framework in big data processing, supports Scala.
  • Streaming applications – Spark which is the fastest data engine is used in streaming applications.
  • Scala also finds its application very widely in Parallel batch processing.

Companies like LinkedIn, Twitter, Netflix use Scala in their products. Hence, Scala is widely used in Big Data, Streaming, web applications. This has led to the recent demand for this language. Scala’s flexibility, parallel processing, simplicity have paved way to its growth in recent days. Scala is one of the most sought after skills for the Java developers and other object-oriented programmers.

Future demand for Scala

As seen above, Scala is already into a wide range of applications. Its strong features and high performance capabilities have made it gain an important place. Its interoperable capabilities with the other languages have given it a chance to enter the already existing applications and now it is seen in a high usage.

Learn from the Experts

Scala offers promising career worldwide. All that you need is expert training and guidance to master the language. GangBoard, the number one Online Training Institute offers excellent Scala Online Training. Get in touch with their team and reap the benefits of their guidance. All the best!!

Social Share:

0 responses on "What is Scala?"

Leave a Message

Your email address will not be published. Required fields are marked *