Introduction To Scala – A Multi Paradigm Programming Language

0
5
Scala

Scala language is a multi-paradigm programming language which is specifically designed to express common programming patterns in a brief, stylish, and type-safe manner. Scala language has been developed by Martin Odersky where the first version was released in the year 2003. Scala language is a combination of both object-oriented and functional languages.

Scala Programming is somewhat related to Java language where the Scala syntaxes resemble Java syntaxes. In Scala programming, the programmer can easily use the Java SDK classes, custom Java classes, and Java open source projects. Therefore, Java developers can easily learn Scala programming.

Features of Scala Language

Scala is a multi-paradigm programming language with the following key features.

1. Scala is an object-oriented Programming language: In Scala programming language, every value is an object where its type and behavior are described by class and trait respectively. Therefore, it can be considered as an object-oriented programming language. It supports inheritance where classes can be extended by sub-classing.
2. Scala is statically typed language: In Scala programming, you need not to specify a type in many cases, therefore, it discourages redundant type information.
3. Scala is a functional language: In Scala language, every function is a value and every value is an object. This implies to the fact that every function is an object. Therefore, Scala can be considered as a functional language. It has a lightweight syntax in order to define anonymous functions. Also, it supports higher-order functions. The functions in Scala can be nested, and support currying.
4. Scala language can Execute Java Code: In Scala programming, you can easily use the Java SDK classes, custom Java classes, and Java open source projects.
5. Scala runs on the JVM (Java Virtual Machine): Scala code is first compiled into Java Bytecode followed by the execution by JVM (Java Virtual Machine) with ‘scala’ command. Due to this nature of Scala language, Java developers can easily learn Scala programming.
6. Scala language can perform Concurrent and Synchronized processing: Scala language supports compact coding which reduces the number of source code lines in a type-safe way. It supports immutable programming practices, therefore, it can perform concurrent and synchronized processing i.e. parallelism.

Environment Setup for Scala

Scala environment can be set up on machines based on Linux or Windows operating system. The following are the steps to set up Scala on Windows Operating System based machines.

Step 1: Java Installation: Scala requires at lease Java 1.8 i.e. Java Software Development Kit (SDK) in order to operate Scala on your machine. Java can be downloaded and installed for free on your machine from the below link.

Java Download Link

http://www.oracle.com/technetwork/java/javase/downloads/index.html

When java is installed on your machine, you can verify the Java version by simply opening the windows command line and issuing the following command.

Java Version verification

Microsoft Windows [Version 10.0.17134.345]

(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\aparajita>java -version

java version “1.8.0_151”

Java(TM) SE Runtime Environment (build 1.8.0_151-b12)

Java HotSpot(TM) Client VM (build 25.151-b12, mixed mode, sharing)

C:\Users\aparajita>

Step 2: Set up environment variables for JAVA_HOME
You need to go to control panel and search for environment variables in order to add the JAVA_HOME and PATH variables as shown below. JAVA_HOME variable should point to the home path where Java is installed and next, you have to append the full path of Java compiler location to the System Path.

Variable

Value

JAVA_HOME

C:\ProgramFiles\java\jdk1.8.0_151

PATH

%PATH% C:\ProgramFiles\java\jdk1.8.0_151\bin

Step 3: Scala installation

In this step, click on the Scala download link below and download the Scala 2.11.12 installer.

Scala Download Link

https://www.scala-lang.org/download/

The installer will start the installation with a below welcome message, click Next.

installation window

Here, you will be asked to read end user license agreement and accept the Scala license. Check the check box and click Next.

check box

Next, select the required location for the installation of Scala binaries. Here, we have selected the path ‘C:\Program Files (x86)\scala’ as shown below. Click Next.

select path

At this step, Scala installation has started. This step will take a couple of minutes to complete.

installing scala programming

last but not the least, click on the Finish button to complete the Scala installation on your machine as shown below.

end the installation

Step 4: Scala Verification

You need to make sure that Scala has included under PATH environment variable as shown below. If it is not added by the installer, then you need to add it manually.

Variable

Value

PATH

%PATH% C:\Program Files (x86)\scala\bin

 

Next, open a Windows command line and issue the following command to verify that Scala has installed successfully.

Scala Version verification

Microsoft Windows [Version 10.0.17134.345]

(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\aparajita>Scala -version

Scala code runner version 2.11.12 — Copyright 2002-2017, LAMP/EPFL

C:\Users\aparajita >

My First Program in Scala

A Scala program can be executed in two modes.
1. Interactive mode: In interactive mode, you can open the Windows command prompt and use the following command to open Scala. Next, you can execute simple Scala commands on Scala prompt by pressing enter as shown below.

Scala Interactive Mode

Microsoft Windows [Version 10.0.17134.345]

(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\aparajita>scala

Welcome to Scala 2.11.12 (Java HotSpot(TM) Client VM, Java 1.8.0_161).

Type in expressions for evaluation. Or try: help.

scala> println (“Hello Scala Programming!”);

Hello Scala Programming!

scala>

 

2. Script mode: In the script mode, you can open a notepad and write the script into this file and save the file as ‘HelloScala.scala’ i.e. scala extension. The Script is shown below.

Scala Script Mode [HelloScala.scala]

object HelloScala {

/* This is my first Scala program.

* This will print ‘Hello Scala World’ as the output.

*/

def main (args: Array[String]) {

println (“Hello Scala World!”) // prints Hello Scala World

}

}

Next, we can compile this file with command ‘scalac HelloScala.scala’ and post successful compilation, we can execute this file with command ‘scala HelloScala’ as shown below.

Scala Script Mode [HelloScala.scala]

Microsoft Windows [Version 10.0.17134.345]

(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\aparajita>scalac C:\scala\HelloScala.scala

C:\Users\aparajita >scala HelloScala

Hello Scala World!

C:\Users\aparajita >

Conclusion

In this article, we discussed about Scala as multi-paradigm languages, and describe its key features. Also, we set up Scala environment on Windows machine and executed our first Scala program into two modes i.e. interactive and script mode.

LEAVE A REPLY

Please enter your comment!
Please enter your name here