What is Scala programming language?

 


What is Scala?

Scala stands for Scalable Language. It is a multi-paradigm programming language. Scala language includes features of functional programming and object-oriented programming. It is a statically typed language. Its source code is compiled into bytecode and executed by Java virtual machine(JVM).

Scala is object-oriented

Scala is an object-oriented language. In Scala, every value is an object.

Scala execution 

Scala uses Java Virtual Machine (JVM) to execute bytecode. Its code is compiled into bytecode and executed by Java Virtual Machine. So you need only JVM to start development with it. Scala can also use all java classes and allows us to create our custom class.

Why use Scala?

It is designed to grow with the demands of its user, from writing small scripts to building a massive system for data processing.

Scala is used in Data processing, distributed computing, and web development. It powers the data engineering infrastructure of many companies.

Who uses Scala?

Scala language is mostly used by Software engineers and Data Engineers. You’ll see some data scientists using it with Apache Spark to process huge data.

How Scala is different from Java?

  • Nested functions –  It allows us to define a function inside another function.
  • Closures – A function whose return value depends on variables declared outside it of function.
  • Every value is an object.
  • Every operation is a method call.

For example:

1
2
val sumX = 1.+(3)
val sumY = 1 + 3

Output of both is the same.

Let’s understand how we can set up a development environment for Scala. As we know, Scala uses JVM. So in order to execute a Scala program, you need to have java installed on your machine.

sudo apt-get update

Now type: sudo apt-get install openjdk-8-jdk

Scala Basics

Scala is very similar to Java  Language, and both use Java Virtual Machine(JVM) to execute code. So, learning Scala would be super easy for you if you have a solid understanding of the Java language. But it’s not mandatory to know Java before learning the Scala Language.

Let’s write our first Scala program!

1
2
3
4
5
object HelloWorld {
  def main(args: Array[String]) {
    println("Hello world!")
  }
}

Output:

Note if you want to execute  this using terminal, then follow the below steps.

Save this file as HelloWorld.scala

To compile and run, use the below commands:

\> scalac HelloWorld.scala

\> scala HelloWorld

Let’s understand the above code.

It can be noticed that I used the object keyword instead of class. Scala allows us to create a singleton class using the object keyword.

object – Scala doesn’t use static keywords like Java, instead it allows us to create a singleton object.

def main(args: Array[String]) – main() method is compulsory for any Scala Program. Scala starts execution from here.

Case Sensitivity − It is case-sensitive.

Comments in Scala

This language supports single-line and multi-line comments. Multi-line could be nested.

1
2
3
4
5
6
7
8
9
10
11
12
13
def main(args: Array[String]) {
 
  //println(f"my name is $name%s")
  val name:String="Rhaul roy"
   
  /*
  println(f"my name is $name%s")
  println(f"my name is $name%s")
  */
 
 
 
}

Output: If you run above code, it will just create a string variable and will not produce any output.

The Scala Interpreter

The Scala interpreter, an alternative way to run Scala code. Type scala on your terminal to launch Scala interpreter.

In this Scala tutorial, I will not be using the interactive mode to execute any Scala code.

Scala Identifiers

Identifiers are nothing but names used for objects, classes, variables and methods. The important thing is, a keyword cannot be used as an identifier. 

Scala has four types of identifiers.

Alphanumeric Identifiers

These kinds of identifiers start with a letter or an underscore, which can be followed by letters, digits, or underscores. 

Note – The ‘$’ character is a reserved keyword.

Valid identifiers  example

sum, _count,  __1_salary

Invalid illegal identifiers example

$sum, 1count, -index

Operator Identifiers

These kinds of operator identifiers consist of one or more operator characters. 

Valid operator identifiers 

+  ++ :::

Mixed Identifiers

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

Valid mixed identifiers

unary_-

unary_-  used as a method name defines a unary – operator. Scala supports unary prefix operators (-, +, ~, !)  only.

Literal identifier

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

Example: 

`xyz` 

0 Response to "What is Scala programming language?"

Post a Comment

Article Top Ads

Central Ads Article 1

Middle Ads Article 2

Article Bottom Ads