Variables are nothing but reserved memory locations to store values.
This means that when you create a variable, you reserve some space in
memory.
Based on the data type of a variable, the compiler allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
Based on the data type of a variable, the compiler allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
Variable Declaration
Scala has a different syntax for declaring variables. They can be defined as value, i.e., constant or a variable. Here, myVar is declared using the keyword var. It is a variable that can change value and this is called mutable variable. Following is the syntax to define a variable using var keyword −Syntax
var myVar : String = "Foo"Here, myVal is declared using the keyword val. This means that it is a variable that cannot be changed and this is called immutable variable. Following is the syntax to define a variable using val keyword −
Syntax
val myVal : String = "Foo"
Variable Data Types
The type of a variable is specified after the variable name and before equals sign. You can define any type of Scala variable by mentioning its data type as follows −Syntax
val or val VariableName : DataType = [Initial Value]If you do not assign any initial value to a variable, then it is valid as follows −
Syntax
var myVar :Int; val myVal :String;
Variable Type Inference
When you assign an initial value to a variable, the Scala compiler can figure out the type of the variable based on the value assigned to it. This is called variable type inference. Therefore, you could write these variable declarations like this −Syntax
var myVar = 10; val myVal = "Hello, Scala!";Here, by default, myVar will be Int type and myVal will become String type variable.
Multiple assignments
Scala supports multiple assignments. If a code block or method returns a Tuple (Tuple − Holds collection of Objects of different types), the Tuple can be assigned to a val variable. [Note − We will study Tuples in subsequent chapters.]Syntax
val (myVar1: Int, myVar2: String) = Pair(40, "Foo")And the type inference gets it right −
Syntax
val (myVar1, myVar2) = Pair(40, "Foo")
Example Program
The following is an example program that explains the process of variable declaration in Scala. This program declares four variables — two variables are defined with variable declaration and remaining two are without variable declaration.Example
object Demo { def main(args: Array[String]) { var myVar :Int = 10; val myVal :String = "Hello Scala with datatype declaration."; var myVar1 = 20; val myVal1 = "Hello Scala new without datatype declaration."; println(myVar); println(myVal); println(myVar1); println(myVal1); } }Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala \>scala Demo
Output
10 Hello Scala with datatype declaration. 20 Hello Scala without datatype declaration.
No comments:
Post a Comment