Big Data Hadoop & Spark

Scala Tutorial Part 1- Basics of Scala

In this blog post we will be discussing about the basics of Scala programming like REPL, commenting in Scala, semi colon inference, keywords that are present in Scala, packages and how to import them, Declarations and finally the Data types.

First we will look the Scala REPL. REPEL stands for Read Evaluate Print and Loop, it means Read function takes the expression as an input from the user and stores the data structure in the memory and the evaluate function ,evaluates the expression and the print function displays the result on the console.

100% Free Course On Big Data Essentials

Subscribe to our blog and get access to this course ABSOLUTELY FREE.

Scala REPL

Let us see this REPL with some examples.

Now we will pass some inputs to the Scala shell and we will check what outputs we are getting.
Firstly we will print “Hello World!” which is the basic for any programming language. We can print it very easily in Scala by just typing

print(“Hello World!”)

We don’t need to give the semi colons also. Refer the below screen shot to find how to print this Hello World statement in different ways.

Now let us pass some expressions in the shell and see the output.
Now we will just give 2+3 the Scala shell will automatically print 5 it reads the expression from the user and evaluates and then print it as an Int
Now let us combine two string “2”+”3”, this time the Scala shell will return a string as “23” it automatically detects that the things which are inside the double quotes as strings and combines the two strings as we are using a concatenate operator.
If we combine a string and an integer like “2”+3, this time also the shell will return the output as a string itself as concatenation of string and an integer results in a string.
Now we will give a mathematical calculation 10*2+4 the shell returns the result as 24 by using the BODMAS rule, it automatically calculates the calculation and returns an Integer as result.

Refer the below screen shot for the same.

Main method in Scala

In Scala we define the method with the below syntax

def main(args:Array[String]){
}

This main method should be written inside the Scala object, in Java we use to write this main method inside the Class and use to declare it as Static because the main method in Java should be accessible to all the other classes also. But here in Scala we do not have the concept of Static so the main method which is written inside the object is accessible within the object itself.

Commenting in Scala

There are two types of comments that can be possible in Scala Single line commenting and Multi line commenting.

Single line comments

We can write single line comments in Scala by just typing // before the line.

Multi line comments

We can write multi line comments in Scala by writing the code in between /*–comments–*/.

Semi colon Inference

In Scala semi colon is not mandatory, we can keep semi colons at the end of the line or we can not keep, it is up to the programmer because if we write a single line of code, the Scala compiler will automatically detects the end of line. So it is not mandatory to represent the end of line using a Semi colon in Scala.
But if we you are writing multiple statements in a single line, then you need to separate them necessarily with a semi colon.

Let us see the same with an example

val value1=”value1”

This will return a string and we need to give any semi colon at the end as it as a single statement.
Now

val value2 = “value2”;print(value2)

Here we need to separate the two statement with a semi colon, as the assignment statement and printing statement are separate.
If we do not give the semi colon in between the two statements, it will return an error.

Refer the below screen shot for the same.

Key words

The below are the different types of keywords that are available in Scala

Packages

Hadoop

A package is a collection of classes and objects, as like in other languages Scala also uses packages to make the classes present collectively. So that we can uniquely identify a class in the project because in a project we will be having so many number of classes and some times we may get a confusion that which class belongs to which module in order to segregate them we uses this packages.
We can create package in Scala using the IDE and we can create as many classes and objects we want inside the package.
First we need to create a Scala project, and then by Right clicking on the src, we can create the package

After creating the Package, we can create an object inside the package.

Now inside the object, we can write our code.

To Run the program Right click on the class–>Run As–>Scala program

Importing Packages

We can import the package using the import statement

import package_name

Here in this package the syntax will be like

import acadgild_scala

We can also import the Java packages to Scala as Scala runs on the same JVM and has the features of object oriented also, Java packages are perfectly compatible with Scala.

To import one class, we can use the syntax like

import acadgild_scala.Hello_World.class

To import all the classes inside the package, we write

import acadgild_scala._

We can import the java libraries by writing

import java.io._

Declarations

There are only two types of declarations in Scala, val and var. In other languages we need to write the data type which we are using to declare the variable but in Scala we do not need to type the data type to declare a variable.
Val is like a constant in Java once we declare a variable with val in Scala, we can not modify it again.
Var is just like a variable we can modify it any number of times depending on our requirement.

Refer the below screen shot for the same

In the above screen shot we can see that we have declared the variable i as val and when we try to increment i an error occurred and then we have declared the variable j as var and when we try to add 1 to it, it got successfully incremented.

Data Types

The below are the data types that are available with Scala
We can check the data type of a variable in Scala by just typing :t variable_name
We can also convert the the variable from one data type to another data type by just typing
variable_name.todata_type
Refer the below screen shot for the same

We can see the available data types that the variable can be converted into by just typing
variable_name. double tab
We can see the output in the above screen shot we have got toLong, toString, toChar, toFloat, toDouble and many more.
In our next blog in the Scala series, we will be discussing about Arithmetic, relational, logical operators, if­else statement, while loops, For loops and methods.
Hope this blog helped you in understanding the basics of Scala, Keep visiting our site www.acadgild.com for more updates on Bigdata and other technologies. Click here to learn Apache Spark from our Expert Mentors
Hadoop

3 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close