Cloud Computing - SCALA

Back to Course

Lesson Description

Lession - #662 SCALA Variables


Factors are only saved memory areas to store values. This implies that when you make a variable, you hold some space in memory.

In view of the information sort of a variable, the compiler allocates memory and concludes what can be put away in the saved memory. Accordingly, by appointing various information types to factors, you can store whole numbers, decimals, or characters in these factors.

Variable Declaration

Scala has an alternate syntax for announcing factors. They can be characterized as value, i.e., consistent or a variable. Here, myVar is declared utilizing the keyword var. A variable can change value and this is called mutable variable. Following is the syntactical structure to characterize a variable utilizing var keyword − Syntax

var myVar : String = "Foo"

Here, myVal is announced utilizing the keyword val. This implies that a variable can't be changed and this is called immutable variable. Following is the syntax to characterize a variable utilizing val keyword − syntax

val myVal : String = "Foo"


Variable Data Types

The sort of a variable is indicated after the variable name and before approaches sign. You can characterize any kind of Scala variable by referencing its information type as follows − syntax

val or val VariableName : DataType = [Initial Value]

In the event that you appoint no underlying worth to a variable, then, at that point, it is substantial as follows − Syntax

var myVar :Int;
val myVal :String;
Variable Type Inference

At the point when you appoint an underlying worth to a variable, the Scala compiler can sort out the kind of the variable in light of the worth relegated to it. This is called variable sort derivation. Subsequently, you could compose these variable revelations like this − syntax

var myVar = 10;
val myVal = "Hi, Scala!";

Here, as a matter of course, myVar will be Int type and myVal will become String type variable.

Multiple assignment

Scala upholds different tasks. On the off chance that a code square or technique returns a (Tuple − Holds assortment of Objects of various kinds>
, the Tuple can be alloted to a val variable. [Note − We will concentrate on Tuples in resulting chapters.] syntax

val (myVar1: Int, myVar2: String>
= Pair(40, "Foo">

Furthermore, the sort derivation takes care of business −

val (myVar1, myVar2>
= Pair(40, "Foo">