Page Contents
Scala variables
Variables are reserved memory locations to store values.
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
In Scala we can declare a variable using var and val keywords. var keyword is used to create a mutable (read-write) variable and val keyword is used to create an immutable (read only) variable.
Syntax – Creating mutable variable
var message = "Foo"
Here, message is declared using the keyword var. That means its a mutable variable and we can reassign a new value to it.
Syntax – Creating immutable variable
val message = "Foo"
Here, message is declared using the keyword val . That means its a immutable variable and we can not reassign a new value to it.
Variable Data Type
Scala supports type inference so there is no need to specify the data type of a variable. But if you really want to specify the type , then it can be specified after the variable name and before equals sign.
Syntax
var VariableName : DataType = [Value] val VariableName : DataType = [Value]
Example
var message:String = "Proedu.co"; val count:Int = 100;
Type Inference
Scala has a built-in type inference mechanism which allows the programmer to omit certain type annotations. It is, for instance, often not necessary in Scala to specify the type of a variable, since the compiler can deduce the type from the initialization expression of the variable.
Type inference is the ability to identify the data type of a variable from the initialization expression of the variable.
Syntax
var count = 10; // Scala can infer the variable type. val message = "Hello, Proedu.co!";
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.
Example – Scala Shell
scala> var a,b,c = 100 a: Int = 100 b: Int = 100 c: Int = 100
Example
object TypeInferenceDemo extends App { // Type inference will work here. var count= 10; val message= "Hello Proedu.co"; //Specifying Data Type of variable var countWithType: Int = 100; val messageWithType: String = "Hello Again Proedu.co"; println(count); println(message); println(countWithType); println(messageWithType); }
Output
10 Hello Proedu.co 100 Hello Again Proedu.co
Variable Scope
Variables in Scala can have three different scopes depending on the place where they are being used. They can exist as fields, as method parameters and as local variables.
Fields
Fields are instance variables that belong to an object. The fields are accessible from inside every method in the object. Fields can also be accessible outside the object depending on what access modifiers the field is declared with. Object fields can be both mutable and immutable types and can be defined using either var or val.
Method Parameters
Method parameters are variables, which are used to pass the value inside a method, when the method is called. Method parameters are only accessible from inside the method but the objects passed in may be accessible from the outside, if you have a reference to the object from outside the method. Method parameters are always immutable which are defined by val keyword.
Local Variables
Local variables are variables declared inside a method. Local variables are only accessible from inside the method.Local variables can be both mutable and immutable types and can be defined using either var or val.