How to Declare Scala Variables – Scope & Examples

Free Scala course with real-time projects Start Now!!

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.

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.

Declaring Scala Variables

We can declare a Scala variable as a constant or as a variable:

a. Constant

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”

b. Variable

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”

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”

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

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")

Scala Variable Scope

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

We have three kinds of scopes for Scala variables. Let’s see them one by one.

a. Fields

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.

Conclusion

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.

Reference

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

2 Responses

  1. Rishabh Shrivastava says:

    Please add examples in point 7

  2. Mahendra Pattnaik says:

    while declaring variables of method parameters in scala why we don’t use var or val

Leave a Reply

Your email address will not be published. Required fields are marked *