This tutorial is based on windows system.
-
Step 1 - Verify Java version
C:\Users\Your_PC_username>java -version
Output
java version "1.8.0_111" Java(TM) SE Runtime Environment (build 1.8.0_111-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
If Java is not installed then install latest Java SDK.
SDK Download -
Step 2 - Install Scala
Scala Download
Run the downloaded .msi file. Follow below screenshots
Click on the NEXT button
Check the “I Agree Option” and then click NEXT
Click on the INSTALL button.
Click on the FINISH button
-
Step 3 - Test Installation
C:\Users\Your_PC_username>java -version
Declare variable and types
Immutable Variable
Meaning - values which can not be changed
Declaration - val :scala:Type = <some_Literal>
Example - val name:String = 'Scala'
Note - Reassignment is not possible.
Mutable variable
Meaning - value can be changed
Declaration - var :scala:type=<some_literal>
Example - var name:String = 'Scala'
Lazy Initialization
Meaning - Call by need means expression is not evaluated until it is needed
Declaration - lazy val = <some_literal>
Pros
- Optimize computation, avoid wasted of CPU
- resolve circular dependencies
Cons
- Tricky in finding bugs
- Goes against conventional approach
Blocks
Meaning - Combine expression by surrounding them {}. Last value will be evaluated in this case.
Declaration - println({ val x =1+23; x+1})
Functions
Meaning - Expression which have parameters, and take arguments
Declaration - val multipleBythree = (x:Int) => x*3
Method
Meaning - Similar to functions but method is always initialized with keyword def and wil always return a value
Declaration - def multipleBythree(x:Int):Int = x*3
Type Hierarchy
What is Nothing in bottom
It is a subtype of everything of higher types. Commonly used to signal non-termination such as exception thrown, exit program or infinite loop.
Type Casting
Class
Meaning - A Scala class is defined with keyword class along with name and constructor parameters
Declaration -
class name(firstName:String,lastName:String){
def fullName(greet:String):Unit =
println(greet+' '+firstName+' '+lastName)
}
val textPrint = new name("Adarsh","Shukla")
textPrint.fullName("Welcome to Canada!!!!!")
}