• date 17th January, 2022 |
  • by Prwatech |

Introduction | Tour of Scala

  Scala is a strong statically typed general-purpose programming language which supports both object-oriented programming and functional programming. Designed to be concise, many of Scala's design decisions are aimed to address criticisms of Java.  Scala started from 2001. Founder of Scala is also the founder of Javac. Started working with Java Compiler. Martin Odersky is the founder of Scala. Scala term collected from “Scalable” . It is scalable language.  Question: Comparison of Scala with OOPS:  Answer:  
  • Scala is more concise, elegant and easy to read/write. 
  • In Scala I line code, is equal to 10 line of Java. 
  • Scala is a Pure object oriented language. 
  • Scala doesn't support "static" keyword. Instead, we have singleton object in Scala. 
  • If your class and object Name is same. Then that class is known as companion class. 
  • Scala does not complain it's complier if data type is not given to variable. 
  • Scala function does not require any return type. unless function is recursive. 
Scala Comparison with Functional programming language (Python, ruby): - 
  • In any functional programming language, by default they support immutability. Though, Scala gives choice to user. 
  • In Scala, functions are first class citizens. 
  • Lazy evaluation  
  • Futures. 
  • Closures. 
  1. Play: Web based app 
  1. Scalding: It means Scala + Cascading (Hibernate) 
  1. Spark: In memory 
  1. Akka : Multi-threading  
  1. Kafka: mq 
  1. Neoyj : Graph DB 

Question: Difference between immutable and mutable  

Answer: Immutable means not changeable. In Scala for assign the immutable value we use val    val = immutable means can’t change   Example:  
val s ="Scala" 

val f = 201.67
  Mutable means changeable. In Scala for assign the mutable value we use var  var= mutable means can change   Example:
var name = "Prwatech" 

name= "Tech"
    Important points: 
  • This operation is happened on RAM. Current session where we write code and run is known as REPL (Read evaluate print loop). 
  • : q use to go out from the cell 
  • System hold one address of x which we call memory address. 
  • X: mapped with some number. 
  • Memory address that depends internal algorithm  
  Immutable and mutable   val: for immutable   var: for mutable   here some other application  Example:  
val x= 1 to 10
  x: scala.collection.immutable.Range.Inclusive = Range (1,2,3,4,5,6,7,8,9,10)  Range:  
  • Range is immutable  
  • Range is created by default immutable  
Immutable has two internal concepts. 
  1. First logic you cannot grow, sink the size once its defined. 
  1. Always index stat 0 
  1. However, you may change index value. 

Question: How can access index? 

Answer:   It always starts from 0  Example:  

 Int =1 


 Int =2 


java.lang.IndexOutOfBoundsException :10 


 Int =10
    if we range this 9 index value from 9 to 100   
    <console>:13: error: value update is not a member of scala.collection.immutable.Range.Inclusive   

 val x1=1 until 10
  x1:scala.collection.immutable.Range= Range(1,2,3,4,5,6,7,8,9)  until 10 means last number excluded.  scala>x1  res5:scala.collection.immutable.Range=Range(1,2,3,4,5,6,7,8,9)   
 val y =1 to 10 by 2
    y:scala.collection.immutable.Range=Range(1,3,5,7,9)  it will create the same size of y but skipping step size by 2  blo belongs to integer data type    
 val blo :{var a=20;var b=20;b-a} 

blo :Int =0 

 val blo :{var a=20;var b=30;b-a} 

blo :Int =10
    here Return function is optional.  Return function   Return function is only using when function is recursive. Recursive means function calling itself. Factorial functions are recursive function.   
 var con= if(blo>9 && blo<25) “one” else 0
    con: Any = one  One is a string data type and 0 is an integer data type. Here we are using mixed data type it will throw compilation error.  It will give compilation error because two different data type is there in your written statement.  Scala say when there are mixed data type not necessary string and int, it can be long and double, Int and string, Float and string any combination with two different families.  Whenever mixed data type hence there supertype is “Any”   Whenever program is stuck we use supertype Any. But from the optimization site is not good. And when we use Any .it will have more range than this two.  Nutshell:   If there are two different datatypes which are mixing two each other from two different families. We can use “ANY”. 


  • Lazy evaluation is one of the terrific concept in all the programming language. In fact, to speed up website performance to perform up scaling, down scaling on server. Everywhere we are using this Lazy evaluation. 
  • Lazy in fact given server less architecture using lambda and other things  
  • Lazy applied with product architecture. 
How Lazy Evaluation works :   
 var opt1: Int =300  

Opt1: Int =300
  • Opt1 is a variable reference for us and this 300 has some memory address on JVM (Java virtual machine) Because compilation of Scala is on Java virtual machine. 
  • When we want 300 call opt1 and this is assigned in memory. So this memory we can call RAM. 
  • Whenever we do program or anything we perform it always goes to first volatile space after volatile then they go to permanent which is disc operation. 
  • Once it goes to RAM immediately it goes to CPU after this memory and try to execute this 300. Whatever is the size of this 300. Let if we consider Int the size of Int 4 byte of this go to CPU and occupy CPU. Some space of CPU.some % of CPU and try to execute 4 bytes and then will give one Answer 300. 
Scala> lazy var   It will have stopped execution. CPU request will have stopped. The Lazy does not work with var. This is architectural acceptance itself.  Lazy var(not applicable ) 
  • Var is mutable and with mutable Lazy is not applicable . 
Var x=10 



  Var is mutable x can be 10,20,30,40. If this is keep changing performance will degrade and hence this have discarded the concept of var  Lazy val(applicable ) 
  • Only val can be Lazy. 
Operation:   There are two operations. 
  • Transformation: when we are not requesting to CPU  
  • Action: requesting to CPU 
Every execution is taking 1 minute.   

How to read file in SCALA? 

  Scala> val f1 =file handling operation(“file path”)function to see the data  Scala>val f1 =“file path.txt”).mkString  If it throws error FileNotFound Exception  Then we will create file first then use syntax to read a file   Conditional Expression:  Total 7 conditional expressions. 
  1. If  
  1. If else 
  1. Nested if else  
  1. If else ladder 
  1. For  
  1. While  
  1. Break  
How to apply for or inner forloop    
 val flop = for(i<-1 to 3)println(i)
    1  2  3  Floop:unit()         Operators: 
  • Arithmetic operator 
  • Logical operator  
Logical operator  
  • And (&&): Both conditions true result will be true else false  
  • OR (||): Any one of conditions true result will be true else false 
  • NOT(!)  
Even vale works on the concept of mod      
 val flop =for (i<-1 to 10;if(i%2==0)println(i)
  2  4  6  8  10  Floop: unit ()  Unit:void type  floop, for loop is type of unit. Why it is not storing anything because using println statement. Println just print that in the console not actually storing anything in this loop.  IF we want to that another operation. The function name yield. This will give the output and store in floop .  If want to store it: yield operation  If want doesn’t store use println    Introduction | Tour of Scala

Quick Support

image image