Basics of Scala Programming Language

Last updated on May 30 2022
Shakuntala Deskmukh

Table of Contents

Basics of Scala Programming Language

Scala – Basic Syntax

If you’ve good understanding on Java, then it’ll be very easy for you to learn Scala. The biggest syntactic difference between Scala and Java is that the ‘;’ line end character is optional.

When we consider a Scala program, it can be defined as a collection of objects that communicate via invoking each other’s methods. Let us now briefly look into what do class, object, methods and instance variables mean.

  • Object − Objects have states and behaviors. An object is an instance of a class. Example − A dog has states – color, name, breed as well as behaviors – wagging, barking, and eating.
  • Class − A class can be defined as a template/blueprint that describes the behaviors/states that are related to the class.
  • Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Fields − Each object has its unique set of instance variables, which are called fields. An object’s state is created by the values assigned to these fields.
  • Closure − A closure is a function; whose return value depends on the value of one or more variables declared outside this function.
  • Traits − A trait encapsulates method and field definitions, which can then be reemployed by mixing them into classes. Traits are employed to define object types by specifying the signature of the supported methods.

First Scala Program

We can execute a Scala program in two modes: one is interactive mode and another is script mode.

Interactive Mode

Open the command prompt and use the subsequent command to open Scala.

\>scala

If Scala is installed in your system, the subsequent output will be displayed −

Welcome to Scala version 2.9.0.1

Type in expressions to have them evaluated.

Type :help for more information.

Type the subsequent text to the right of the Scala prompt and press the Enter key −

scala> println("Hello, Scala!");

It’ll produce the subsequent result −

Hello, Scala!

Script Mode

Use the subsequent instructions to write a Scala program in script mode. Open notepad and add the subsequent code into it.

Example

object HelloWorld {

   /* This is my primary java program. 

   * This will print 'Hello World' as the output

   */

   def main(args: Array[String]) {

      println("Hello, world!") // prints Hello World

   }

}

Save the file as − HelloWorld.scala.

Open the command prompt window and go to the directory where the program file is saved. The ‘scalac’ command is employed to compile the Scala program and it’ll generate a few class files in the current directory. One of them will be called HelloWorld.class. This is a bytecode which will run on Java Virtual Machine (JVM) using ‘scala’ command.

Use the subsequent command to compile and execute your Scala program.

\> scalac HelloWorld.scala

\> scala HelloWorld

Output

Hello, World!

Basic Syntax

The subsequent are the basic syntaxes and coding conventions in Scala programming.

  • Case Sensitivity − Scala is case-sensitive, which means identifier Hello and hello would have different meaning in Scala.
  • Class Names − For all class names, the primary letter should be in Upper Case. If several words are employed to form a name of the class, each inner word’s primary letter should be in Upper Case.

Example − class MyFirstScalaClass.

  • Method Names − All method names should start with a Lower Case letter. If multiple words are employed to form the name of the method, then each inner word’s primary letter should be in Upper Case.

Example − def myMethodName()

  • Program File Name − Name of the program file should exactly match the object name. When saving the file you ought save it using the object name (Remember Scala is case-sensitive) and append ‘.scala’ to the end of the name. (If the file name and the object name don’t match your program will not compile).

Example − Assume ‘HelloWorld’ is the object name. Then the file should be saved as ‘HelloWorld.scala’.

  • def main(args: Array[String]) − Scala program processing starts from the main() method which is a mandatory part of every Scala Program.

Scala Identifiers

All Scala components require names. Names employed for objects, classes, variables and methods are called identifiers. A keyword cannot be employed as an identifier and identifiers are case-sensitive. Scala supports four types of identifiers.

Alphanumeric Identifiers

An alphanumeric identifier starts with a letter or an underscore, which can be followed by further letters, digits, or underscores. The ‘$’ character is a reserved keyword in Scala and should not be employed in identifiers.

Subsequent are legal alphanumeric identifiers

age, salary, _value,  __1_value

Subsequent are illegal identifiers

$salary, 123abc, -salary

Operator Identifiers

An operator identifier consists of one or more operator characters. Operator characters are printable ASCII characters such as +, :, ?, ~ or #.

Subsequent are legal operator identifiers −

+ ++ ::: <?> :>

The Scala compiler will internally “mangle” operator identifiers to turn them into legal Java identifiers with embedded $ characters. For instance, the identifier :-> would be represented internally as $colon$minus$greater.

Mixed Identifiers

A mixed identifier consists of an alphanumeric identifier, which is followed by an underscore and an operator identifier.

Subsequent are legal mixed identifiers −

unary_+,  myvar_=

Here, unary_+ employed as a method name defines a unary + operator and myvar_= employed as method name defines an assignment operator (operator overloading).

Literal Identifiers

A literal identifier is an arbitrary string enclosed in back ticks (` . . . `).

Subsequent are legal literal identifiers −

`x` `<clinit>` `yield`

Scala Keywords

The subsequent list shows the reserved words in Scala. These reserved words may not be employed as constant or variable or any other identifier names.

abstract case catch class
def do else extends
false final finally for
forSome if implicit import
lazy match new Null
object override package private
protected return sealed super
this throw trait Try
true type val Var
while with yield
: = =>
<- <: <% >:
# @

Comments in Scala

Scala supports single-line and multi-line comments very similar to Java. Multi-line comments may be nested, but are required to be properly nested. All characters available inside any comment are ignored by Scala compiler.

object HelloWorld {

   /* This is my primary java program. 

    * This will print 'Hello World' as the output

    * This is an example of multi-line comments.

    */

   def main(args: Array[String]) {

      // Prints Hello World

      // This is also an example of single line comment.

      println("Hello, world!")

   }

}

Blank Lines and Whitespace

A line containing only whitespace, possibly with a comment, is known as a blank line, and Scala totally ignores it. Tokens may be separated by whitespace characters and/or comments.

Newline Characters

Scala is a line-oriented language where statements may be terminated by semicolons (;) or newlines. A semicolon at the end of a statement is usually optional. You can type one if you want but you don’t have to if the statement appears by itself on a single line. On the other hand, a semicolon is required if you write multiple statements on a single line. Below syntax is the usage of multiple statements.

val s = “hello”; println(s)

Scala Packages

A package is a named module of code. For example, the Lift utility package is net.liftweb.util. The package declaration is the primary non-comment line in the source file as follows −

package com.liftcode.stuff

Scala packages can be imported so that they can be referenced in the current compilation scope. The subsequent statement imports the contents of the scala.xml package −

import scala.xml._

You can import a single class and object, for example, HashMap from the scala.collection.mutable package −

import scala.collection.mutable.HashMap

You can import more than one class or object from a single package, for example, TreeMap and TreeSet from the scala.collection.immutable package −

import scala.collection.immutable.{TreeMap, TreeSet}

Apply Dynamic

A marker trait that enables dynamic invocations. Instances x of this trait allow method invocations x.meth(args) for arbitrary method names meth and argument lists args as well as field accesses x.field for arbitrary field namesfield. This feature is introduced in Scala-2.10.

If a call is not natively supported by x (i.e. if type checking fails), it is rewritten according to the subsequent rules −

foo.method("blah") ~~> foo.applyDynamic("method")("blah")

foo.method(x = "blah") ~~> foo.applyDynamicNamed("method")(("x", "blah"))

foo.method(x = 1, 2) ~~> foo.applyDynamicNamed("method")(("x", 1), ("", 2))

foo.field ~~> foo.selectDynamic("field")

foo.varia = 10 ~~> foo.updateDynamic("varia")(10)

foo.arr(10) = 13 ~~> foo.selectDynamic("arr").update(10, 13)

foo.arr(10) ~~> foo.applyDynamic("arr")(10)


So, this brings us to the end of blog. This Tecklearn ‘Basics of Scala Programming Language’ helps you with commonly asked questions if you are looking out for a job in Apache Spark and Scala and Big Data Developer. If you wish to learn Apache Spark and Scala and build a career in Big Data Hadoop domain, then check out our interactive, Apache Spark and Scala Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/apache-spark-and-scala-certification/

Apache Spark and Scala Training

About the Course

Tecklearn Spark training lets you master real-time data processing using Spark streaming, Spark SQL, Spark RDD and Spark Machine Learning libraries (Spark MLlib). This Spark certification training helps you master the essential skills of the Apache Spark open-source framework and Scala programming language, including Spark Streaming, Spark SQL, machine learning programming, GraphX programming, and Shell Scripting Spark. You will also understand the role of Spark in overcoming the limitations of MapReduce. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Apache Spark.

Why Should you take Apache Spark and Scala Training?

  • The average salary for Apache Spark developer ranges from approximately $93,486 per year for Developer to $128,313 per year for Data Engineer. – Indeed.com
  • Wells Fargo, Microsoft, Capital One, Apple, JPMorgan Chase & many other MNC’s worldwide use Apache Spark across industries.
  • Global Spark market revenue will grow to $4.2 billion by 2022 with a CAGR of 67% Marketanalysis.com

What you will Learn in this Course?

Introduction to Scala for Apache Spark

  • What is Scala
  • Why Scala for Spark
  • Scala in other Frameworks
  • Scala REPL
  • Basic Scala Operations
  • Variable Types in Scala
  • Control Structures in Scala
  • Loop, Functions and Procedures
  • Collections in Scala
  • Array Buffer, Map, Tuples, Lists

Functional Programming and OOPs Concepts in Scala

  • Functional Programming
  • Higher Order Functions
  • Anonymous Functions
  • Class in Scala
  • Getters and Setters
  • Custom Getters and Setters
  • Constructors in Scala
  • Singletons
  • Extending a Class using Method Overriding

Introduction to Spark

  • Introduction to Spark
  • How Spark overcomes the drawbacks of MapReduce
  • Concept of In Memory MapReduce
  • Interactive operations on MapReduce
  • Understanding Spark Stack
  • HDFS Revision and Spark Hadoop YARN
  • Overview of Spark and Why it is better than Hadoop
  • Deployment of Spark without Hadoop
  • Cloudera distribution and Spark history server

Basics of Spark

  • Spark Installation guide
  • Spark configuration and memory management
  • Driver Memory Versus Executor Memory
  • Working with Spark Shell
  • Resilient distributed datasets (RDD)
  • Functional programming in Spark and Understanding Architecture of Spark

Playing with Spark RDDs

  • Challenges in Existing Computing Methods
  • Probable Solution and How RDD Solves the Problem
  • What is RDD, It’s Operations, Transformations & Actions Data Loading and Saving Through RDDs
  • Key-Value Pair RDDs
  • Other Pair RDDs and Two Pair RDDs
  • RDD Lineage
  • RDD Persistence
  • Using RDD Concepts Write a Wordcount Program
  • Concept of RDD Partitioning and How It Helps Achieve Parallelization
  • Passing Functions to Spark

Writing and Deploying Spark Applications

  • Creating a Spark application using Scala or Java
  • Deploying a Spark application
  • Scala built application
  • Creating application using SBT
  • Deploying application using Maven
  • Web user interface of Spark application
  • A real-world example of Spark and configuring of Spark

Parallel Processing

  • Concept of Spark parallel processing
  • Overview of Spark partitions
  • File Based partitioning of RDDs
  • Concept of HDFS and data locality
  • Technique of parallel operations
  • Comparing coalesce and Repartition and RDD actions

Machine Learning using Spark MLlib

  • Why Machine Learning
  • What is Machine Learning
  • Applications of Machine Learning
  • Face Detection: USE CASE
  • Machine Learning Techniques
  • Introduction to MLlib
  • Features of MLlib and MLlib Tools
  • Various ML algorithms supported by MLlib

Integrating Apache Flume and Apache Kafka

  • Why Kafka, what is Kafka and Kafka architecture
  • Kafka workflow and Configuring Kafka cluster
  • Basic operations and Kafka monitoring tools
  • Integrating Apache Flume and Apache Kafka

Apache Spark Streaming

  • Why Streaming is Necessary
  • What is Spark Streaming
  • Spark Streaming Features
  • Spark Streaming Workflow
  • Streaming Context and DStreams
  • Transformations on DStreams
  • Describe Windowed Operators and Why it is Useful
  • Important Windowed Operators
  • Slice, Window and ReduceByWindow Operators
  • Stateful Operators

Improving Spark Performance

  • Learning about accumulators
  • The common performance issues and troubleshooting the performance problems

DataFrames and Spark SQL

  • Need for Spark SQL
  • What is Spark SQL
  • Spark SQL Architecture
  • SQL Context in Spark SQL
  • User Defined Functions
  • Data Frames and Datasets
  • Interoperating with RDDs
  • JSON and Parquet File Formats
  • Loading Data through Different Sources

Scheduling and Partitioning in Apache Spark

  • Concept of Scheduling and Partitioning in Spark
  • Hash partition and range partition
  • Scheduling applications
  • Static partitioning and dynamic sharing
  • Concept of Fair scheduling
  • Map partition with index and Zip
  • High Availability
  • Single-node Recovery with Local File System and High Order Functions
Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Basics of Scala Programming Language"

Leave a Message

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