How to Declare Scala Variables – Scope & Examples
Stay updated with latest technology trends
Join DataFlair on Telegram!!
1. Scala Variables – Objective
What are Scala variables, and how do we declare them? What is type inference? How do we perform multiple assignments? What is the variable scope? These are the questions we’ll be answering today.
So, let’s start Scala Variables Tutorial.
2. Introduction to Variables in Scala
A variable is a reserved memory location to store values. The compiler allocates memory and decides what goes in based on a variable’s data type. We can assign different types like integers, characters, or decimals to variables. Let’s begin.
3. Declaring Scala Variables
We can declare a Scala variable as a constant or as a variable:
To declare a value that we cannot mutate/change, we use the keyword ‘val’. This is immutable.
val x:Int=7 val name:String=”Ayushi”
A variable is mutable. We can change its value as many times as we like. Declare it using the keyword ‘var’.
var name:String=”Ayushi” name=”Megha”
4. Scala Variables Type Inference
When we assign an initial value to a variable, the compiler infers its type based on the types of the subexpressions and the literals. This means that we don’t always have to declare the type of a variable.
val x=1+2 var name=”Ayushi”
5. Scala Variable Data Types
We can specify a variable’s type after a colon after its name. Take an example:
val x:Int=7 val x:Int
6. Multiple Assignments
How do we fit multiple assignment statements into one statement? We assign a Tuple to a val variable.
val (x: Int, y: String) = Pair(7, "Ayushi") val (x, y) = Pair(7, "Ayushi")
7. Scala Variable Scope
We have three kinds of scopes for Scala variables. Let’s see them one by one.
If an object owns a variable, the variable is a field in it. We can access fields from inside every method in the object. Well, we can also access them outside the object if we declared them with the right access modifiers. A field may be mutable or immutable, and we can define them using ‘var’ or ‘val’.
b. Method Parameters
A method parameter is a variable that we can use to pass a value inside a method when we call it. We can only access it inside the method, but if we have a reference to that object from outside the method, we can access the objects from outside. A method parameter is always immutable, and we define it using the ‘val’ keyword.
c. Local Variables
A local variable is one we declare inside a method. While we can only access it from inside a method, if we return the objects, that we create, from the method, they may escape it. A local variable may be mutable or immutable, and we may define it using the keywords ‘var’ or ‘val’.
So, this was all about Scala Variables. Hope you like our explanation.
Hence, we discussed about how to declare Scala variables, type inference, data type, and scope. If you have any query regarding Scala Variables, please comment. See you again with another topic to learn with Scala. Have a good day.