SCALA
Introduction
Scala is a modern multi-paradigm
programming language designed to express
common programming patterns in a concise,
elegant, and type-safe way.
It seamlessly integrates features of object-
oriented and functional languages.
SCALA: Classes and Objects
A class is a blueprint for objects.
Once you define a class, you can create
objects from the class blueprint with the
keyword new.
Through the object, you can use all
functionalities of the defined class.
Conti…
Class
Following is a simple syntax to define a basic class
in Scala. This class defines two variables x and y
and a method: move, which does not return a value.
Class variables are called, fields of the class and
methods are called class methods.
The class name works as a class constructor which
can take several parameters.
The above code defines two constructor arguments,
xc and yc; they are both visible in the whole body of
the class.
Conti…
Syntax
class Point(xc: Int, yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int) {
x = x + dx
y = y + dy
println ("Point x location : " + x);
println ("Point y location : " + y);
}
You can create objects using a keyword new and then you can
access class fields and methods
Conti…
Extending a Class
You can extend a base Scala class and you
can design an inherited class in the same way
you do it in Java (use extends keyword), but
there are two restrictions: method overriding
requires the override keyword, and only the
primary constructor can pass parameters to
the base constructor.
Conti…
Implicit Classes
Implicit classes allow implicit conversations with the
class’s primary constructor when the class is in scope.
An implicit class is a class marked with an ‘implicit’
keyword. This feature is introduced in Scala 2.10.
Syntax − The following is the syntax for implicit
classes. Here implicit class is always in the object
scope where all method definitions are allowed
because the implicit class cannot be a top-level class.
Conti…
Syntax
object <object name> {
implicit class <class name>(<Variable>: Data
type) {
def <method>(): Unit =
}
}
Conti…
Singleton Objects
Scala is more object-oriented than Java because,
in Scala, we cannot have static members.
Instead, Scala has singleton objects. A singleton is
a class that can have only one instance, i.e.,
Object.
You create a singleton using the keyword object
instead of the class keyword.
Since you can't instantiate a singleton object, you
can't pass parameters to the primary constructor.
Basic Types and Operators
Sr. No. Data Type Description
8 bit signed value. Range from -128 to
1 Byte
127
16 bit signed value. Range -32768 to
2 Short
32767
32 bit signed value. Range -2147483648
3 Int
to 2147483647
64 bit signed value. -
4 Long 9223372036854775808 to
9223372036854775807
5 Float 32 bit IEEE 754 single-precision float
6 Double 64 bit IEEE 754 double-precision float
Conti…
Sr. No. Data Type Description
16 bit unsigned Unicode character. Range
7 Char
from U+0000 to U+FFFF
8 String A sequence of Chars
9 Boolean Either the literal true or the literal false
10 Unit Corresponds to no value
11 Null null or empty reference
The subtype of every other type; includes no
12 Nothing
values
The supertype of any type; any object is of
13 Any
type Any
14 AnyRef The supertype of any reference type
Conti…
An operator is a symbol that tells the
compiler to perform specific mathematical or
logical manipulations. Scala is rich in built-in
operators and provides the following types of
operators −
◦ Arithmetic Operators
◦ Relational Operators
◦ Logical Operators
◦ Bitwise Operators
◦ Assignment Operators
Conti…
Arithmetic Operators
The following arithmetic operators are
supported by the Scala language.
Operator Description
+ Adds two operands
- Subtracts second operand from the first
* Multiplies both operands
/ Divides numerator by de-numerator
Modulus operator finds the remainder after division of one number by
%
another
Conti…
Relational Operators
The following relational operators are
supported by the Scala language
Operator Description
Checks if the values of two operands are equal or not, if yes then the
==
condition becomes true.
Checks if the values of two operands are equal or not, if values are not
!=
equal then the condition becomes true.
Checks if the value of the left operand is greater than the value of the
>
right operand, if yes then the condition becomes true.
Conti…
Operator Description
Checks if the value of the left operand is less than the value
< of the right operand, if yes then the condition becomes
true.
Checks if the value of the left operand is greater than or
>= equal to the value of the right operand, if yes then the
condition becomes true.
Checks if the value of the left operand is less than or equal
<= to the value of the right operand, if yes then the condition
becomes true.
Conti…
Logical Operators
The following logical operators are supported
by the Scala language.
Operator Description
It is called Logical AND operator. If both the operands are non zero then
&&
the condition becomes true.
It is called Logical OR Operator. If any of the two operands is non zero then
||
the condition becomes true.
It is called Logical NOT Operator. Use to reverses the logical state of its
! operand. If a condition is true then the Logical NOT operator will make it
false.
Conti…
Bitwise Operators
Bitwise operator works on bits and performs bit by bit
operation. The truth tables for &, |, and ^ are as
follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Conti…
Operator Description
Binary AND Operator copies a bit to the result if it exists in both
&
operands.
| Binary OR Operator copies a bit if it exists in either operand.
Binary XOR Operator copies the bit if it is set in one operand but not
^
both.
Binary Ones Complement Operator is unary and has the effect of
~
'flipping' bits.
Binary Left Shift Operator. The bit positions of the value of the left
<< operand are moved left by the number of bits specified by the right
operand.
Binary Right Shift Operator. The bit positions of the left operand value
>>
are moved right by the number of bits specified by the right operand.
Shift right zero-fill operator. The left operands value is moved right by
>>> the number of bits specified by the right operand and shifted values
Conti…
Assignment Operators
There are the following assignment operators
supported by Scala language −
Operato
Description
r
Simple assignment operator, Assigns values from right side operands to
=
left side operand
Add AND assignment operator, It adds the right operand to the left
+=
operand and assigns the result to the left operand
Subtract AND assignment operator, It subtracts right operand from the
-=
left operand and assigns the result to left operand
Multiply AND assignment operator, It multiplies right operand with the
*=
left operand and assigns the result to the left operand
Conti…
Assignment
Operato
Description Operators
r
There are the following assignment operators
Divide AND assignment operator, It divides left operand with the right
/=
supported
operand and by Scala
assigns language
the result to left operand−
Modulus AND assignment operator, It takes modulus using two operands
%=
and assign the result to the left operand
<<= Left shift AND assignment operator
>>= Right shift AND assignment operator
&= Bitwise AND assignment operator
^= bitwise exclusive OR and assignment operator
|= bitwise inclusive OR and assignment operator
THANK
YOU