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 Comparison with Functional programming language (Python, ruby): –
Framework:
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:
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:
Immutable has two internal concepts.
Answer: It always starts from 0
Example:
x(0) Int =1 x(1) Int =2 x(10) java.lang.IndexOutOfBoundsException :10 x(9) Int =10
if we range this 9 index value from 9 to 100
x(9)=100
<console>:13: error: value update is not a member of scala.collection.immutable.Range.Inclusive
x(9)=100. 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”.
How Lazy Evaluation works :
var opt1: Int =300 Opt1: Int =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 x=10 X=20 X=30 X=40
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 )
Operation:
There are two operations.
Every execution is taking 1 minute.
Scala> val f1 =file handling operation(“file path”)function to see the data
Scala>val f1 = scala.io.Source.fromFile(“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.
How to apply for or inner forloop
val flop = for(i<-1 to 3)println(i)
1
2
3
Floop:unit()
Operators:
Logical operator
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