Introduction
Tip #1: Before you install Scala check your java version
Open command prompt and type “java –version” and it shows the version of java that has been installed in your computer.
C:\Users\ADMIN>java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode, sharing)
How to open Scala prompt?
Open command prompt and type command “scala”
If scala is installed in your system, scala version will be displayed as follows.
C:\Users\ADMIN>scala
Welcome to Scala version 2.10.6 (Java HotSpot(TM) Client VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
How to exit scala prompt?
Type “exit” and press Enter
How to write and run Scala programs?
Step 1: Open notepad and type program
Step 2: Save file with .scala extension
Step 3: Open command prompt and go to the directory in which scala program is saved.
Step 4: Type “scala filename.scala”
Program #1: To display “hello world”
File name: helloworld.scala
Scala code:
println("hello world")
Output:
C:\Users\ADMIN\Desktop>scala helloworld.scala
hello world
Program #2: To find sum of numbers in the list
File name: sum.scala
Scala Code:
var total = 0
val numbers = List(1,2,3,4,5,6)
for (e <- numbers){
total += e
}
println(total)
output:
C:\Users\ADMIN\Desktop>scala sum.scala
21
Program #3: To find addition of two numbers
File name: add.scala
Scala Code:
var a=10
var b=20
println("addition is "+(a+b))
Output:
C:\Users\ADMIN\Desktop\scala programs>scala add.scala
addition is 30
Program #4: Demo on for loop
File name: forloop.scala
Scala Code:
var a=0
for(a <- 1 to 10)
println (a)
Output:
C:\Users\ADMIN\Desktop\scala programs>scala forloop.scala
1
2
3
4
5
6
7
8
9
10
Program #5: Multiplying each number in the list by two using map transformation
File name: mapdemo.scala
Scala Code:
val numbers = List (1,2,3,4,5,6)
println(numbers.map{e => e * 2})
println(numbers.map{e:Int => e * 2}
Output:
C:\Users\ADMIN\Desktop\scala programs>scala mapdemo.scala
List(2, 4, 6, 8, 10, 12)
List(2, 4, 6, 8, 10, 12)
Program #6: Demo on scope of a variable
File name: scope.scala
Scala Code:
val x = 2
def f(y: Int) = y +1 //f is a function that increments y by 1
val result = { //local scope started
val x = f(3); //x is assigned with 4
println(x * x) //prints 4*4=16
} //local scope ended
println(x) //prints 4
Output:
C:\Users\ADMIN\Desktop\scala programs>scala scope.scala
16
2
Program #7: Demo on static typing
File name:statictypingdemo.scala
Scala Code:
var greet = "hello" //no need to mention its data type
println(greet)
greet = "how are you"
println(greet)
Output:
C:\Users\ADMIN\Desktop\scala programs>scala statictypingdemo.scala
hello
how are you
Program #8: Difference between val and var
File name:valvardemo.scala
Scala Code:
var greet1 : String = "hello"
println(greet1)
greet1 = "how are you"
val greet2 : String = "hello"
println(greet2)
greet2 = "how are you" //val can not be changed similar to const
Output:
C:\Users\ADMIN\Desktop\scala programs>scala valvardemo.scala
C:\Users\ADMIN\Desktop\scala programs\valvardemo.scala:7: error: reassignment to val
greet2 = "how are you"
^
one error found
Program #9: Demo on classes
File Name:classdemo.scala
Scala Code:
class Student (val year: Int, var grade: Int) //Student represents class
var s1 = new Student (2016,4) //s1 represents object
println(s1.year)
println(s1.grade)
Output:
C:\Users\ADMIN\Desktop\scala programs>scala classdemo.scala
2016
4
Program #10: Demo on constructors and methods
File name: constructordemo.scala
Scala Code:
class Student (val year: Int, var grade: Int)
{
println("created")
def ChangeGrade (no : Int) //method ChangeGrade
{
println("Changing the grade")
grade = no
println("New Grade is :"+grade) //comma or + can be used
}
}
var s1 = new Student (2005,5) //Constructor can be called
s1.ChangeGrade(25); //all three statements do same thing
s1.ChangeGrade(25)
s1 ChangeGrade 25
Output:
C:\Users\ADMIN\Desktop\scala programs>scala constructordemo.scala
created
Changing the grade
New Grade is :25
Changing the grade
New Grade is :25
Changing the grade
New Grade is :25