Scala
Index
1. Scala Installation
2.
3. Variables
4. Data Types
5. Literals
6. Escape sequences
7. Multiple assignments
8. Scope of variables
Spark is a framework. Scala is independent programming language. To work only with Scala, Spark is not required. Spark needs some language like Scala, Java, Python or R language.
1. Scala Installation
First install Java and only then go with Scala installation.
Scala installation flavors
1. Installation on any Linux OS machine (Check whether the Linux is Debian based or RPM based).
2. Installation on any Windows OS machine (Windows 7 on wards with 64 bit).
3. Installation on any Mac OS machine.
Pre-Requisites for Scala installation
1. Check Java is installed or not, along with JDK & JRE, with minimum 1.7 or higher version.
2. Configure the Java bin directory path along with the JRE path.
3. Configure JAVA_HOME because majority of the times Scala looks into JAVA_HOME when executing.
Checking the Java availability
Windows Environment | Linux/ Mac Environment |
C: \java -version C: \javac -version |
$java -version $javac -version |
Scala IDE’s are Intelli J and Eclipse. But initial developers should use SBT (Scala Build Tools) and download using the link (https://www.scala-lang.org/download/)
Click on Windows (SBT-1.2.8.MSI) and proceed with installation process.
2.
3. Variables
vals | vars |
Constants/ Immutable. Similar to final variables in Java. | Non constants/ Mutable. Similar to non-final variables in Java. |
Once initialized, can never be re-assigned within a scope. | Can be re-assigned throughout the application. |
val can be assigned to var | var cannot be assigned to val |
val myvalmsg01 = “This is first value based variable in Scala”; println(myvalmsg01); val myvalmsg02:java.lang.String = “This is my value with custom type inferencing”; val myvalint01 = 65; val myvalint02 :java.lang.Integer = 75; myvalint02 = 85;–Error
|
var myvarmsg01 = “Process is initiating. Please wait..!!”; var myvarmsg01 = “Another instance has started”; println(myvarmsg01); val myvalmsg01 = myvarmsg01; println(myvalmsg01); myvarmsg01 = “You are reassigned”; println(myvarmsg01); myvalmsg01 = myvarmsg01;–Error |
4. Data Types
Scala data types are almost same to Java.
• Number types
Byte | Short | Int | Long | Float | Double |
· 8 bit signed value · Range -128 to +127 |
· 16 bit signed value · Range -32768 to +32767 |
· 32 bit signed value · Range -(2^31) to +(2^31-1) |
· 64 bit signed value · Range -(2^63) to +(2^63-1) |
32 bit IEEE 754 single precision float | 64 bit IEEE 754 double precision float |
• Character types
Char | String |
· 16 bit unsigned unicode character. · Range from U+0000 to U+FFFF |
· Manages a sequence of characters represented in double quotes. |
• Boolean type
· Manages either True or False. |
• Unit type
· Corresponds to no value. Referred as Function return type. Unit represents void in Java. |
• Null type
Null | Nothing |
· Represents a null or empty reference | · The subtype of every other type · Includes no values also |
• Any type
Any | AnyRef |
· The supertype of Any type · Any object is of type Any in Scala operations · The above examples in variables are considered to have any data type. |
· The supertype of Any Reference type |
Note: All data types are Objects in Scala. There are no primitive types in Scala. Hence we can call methods on every type.
5. Literals
Literals means constants
• Integral literals: Usually of type Int or Long, followed by suffix ‘L’. If not suffixed they are treated as integer.
• Floating point literals: Usually of type Float, followed by suffix ‘F’. If not suffixed they are treated as double.
• Boolean literals: Usually of type Boolean and represented as True or False
• Symbol literals: A shorthand for the expression Scala.Symbol(“x”). Symbol is a case class in Scala.
• Character literals: A single character enclosed in single quotes. The character can be either a printable unicode character or is described by an escape sequence.
• Single-line string: A sequence of characters in double quotes. The character can be either a printable unicode character or is described by an escape sequence.
• Multi-line strings: A sequence of characters in enclosed in triple quotes.”””…”””
• Null values: Usually of type Null and is thus compatible with every reference type. This refers to a special “Null” object.
6. Escape Sequences
Escape sequence | Unicode | Description |
\b | \u0008 | Backspace |
\t | \u0009 | Horizontal tab |
\n | \u000c | Form feed |
\f | \u000c | Form feed |
\r | \u000d | Carriage return |
\” | \u00022 | Double quote |
\’ | \u00027 | Single quote |
\\ | \u0005c | Backslash |
7. Multiple assignments
Scala supports multiple assignments and are used to manage data from tuples.
val (myPar1: DataType, myPar2: DataType) = FunctionName(value01, value02);
8. Scope of variables
Three different scopes:
i. Fields
ii. Method parameters
iii. Local variables
i. Fields:
• Fields are variables which belongs to an object.
• They are accessible inside every method declared upon the object.
• They are accessible outside the object depending on the access modifiers applied on the field.
• Object fields can be both mutable and immutable types and can be defined using var or val.