Knoldus Blog Audio
What Is Scala?
Scala is a multi purpose programming language , which provides support for both Object Oriented and Functional Programming. It express common programming patterns in a concise, elegant, and type-safe manner. It was Designed in the year 2003 by Mr. Martin Odersky and his research team.
The most fascinating thing about scala is that , Scala executable code runs over JVM so to run scala at your system you must have Java Software Development Kit (SDK) installed on your system. Scala code is first compiled by a Scala compiler and the byte code for the same is generated, which will be then transferred to the Java Virtual Machine to generate the output.
Why Do We Need It?
Well To Understand This we can have a look on the points mentioned below:-
- Scala is designed to eliminate unnecessary code, It supports multiple Libraries and APIs which saves a lot of time of a Developer.
- It supports Immutable data and it has support to the higher order functions.
- Scala Support’s parallel data processing.
- It also work’s fine with the data which is stored in a Distributed manner.
- Scala also support’s those data which are mutable.
Features of Scala.
- Object Oriented Programming Language:- It is both a functional Programming Language and an object-oriented programming Language. Every variable and value which is used in Scala is implicitly saved as an object by default.
- Functional Programming Language:– It provides a lightweight syntax for defining functions and also it supports higher-order functions, it allows functions to be nested.
- Statically Typed Programming Language:- It means that the types are checked before run-time so mistakes can be caught earlier.
- Extensible Programming Language:- It can multiple language constructs without the need of any Libraries, and APIs.
- Interoperability:-It means that it simply compiles the code using scala compiler and converts code into Java Byte Code and Executes it on JVM.
Variables in Scala:-
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labelling data with a descriptive name, so our programs can be understood more clearly. Similarly we have variables in scala which is divided into two parts which are as follows:-
These variables allow us to change a value after the declaration of a variable. Mutable variables are defined by using the var keyword. The first letter of data type should be in capital letter because in Scala data type is treated as an object.
var exampleOne= "Prashant Trivedi's Blog" exampleOne= "Developer" output:- exampleOne:String = "Prashant Trivedi's Blog" exampleOne:String = "Developer"
Here it will alter the change as shown in the output.
These variables allow us to change a value after the declaration of a variable. Mutable variables are defined by using the val keyword. The first letter of data type should be in capital letter because in Scala data type is treated as an object.
val exampleTwo= "Prashant Trivedi's Blog" exampleTwo= "Developer" output:- exampleTwo:String = "Prashant Trivedi's Blog" <console>:error: reassignment to val exampleTwo= "Developer" ^
Here it will not alter the change rather it will show error as shown in the output.
Lazy Evaluation is the primary feature of scala. It basically means that it an evaluation strategy which delays the evaluation of an expression until its value is needed. And today most of the modern programming languages support Lazy Evaluation.
lazy evaluation can deliver few benefits such as :-
- One benefit using lazy collections is to “save” memory, e.g. when mapping to large data structures.
- It Reduces Complexities , there are two types of complexities of any operations are Time and Space complexity and with the help of lazy evaluation we can overcome both complexities.
- It increase the ability to manage i.e, In Lazy evaluation, Developer/Users can divide into smaller operations. So It reduces the number of passes on data by transformation grouping operation.
It can be understood much easier with the help of example given below:-
Input:- Val exampleThree = List(4,5,6,7) lazy val output = exampleThree .map( 1 => 1*100) println( output ) Output:- List( 400, 500, 600, 700 )