×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala Variables

By IncludeHelp Last updated : October 07, 2024

What are Scala Variables?

Scala variables are the named references to memory locations. The location stores the data that is used by the program. Based on the data type of the variable the memory size allocated is defined.

Type of Scala Variables

Scala allows you to define variables of two types:

  1. Mutable Variables
  2. Immutable Variables

1) Mutable Variables

Variables that allow us to change its value any time in the code. Scala variables are created using the var keyword. You can optionally give the data type of the variable with data type name with first letter capital.

Syntax to declare mutable variables

// Syntax with variable's data type
var  variable_name : Data_type = value;

// Syntax without variable's data type
var  variable_name = value;

Scala example of mutable variables

object MyClass {
  def main(args: Array[String]): Unit = {
    var a: Int = 33;
    var b = 54;

    a = 21; // it will work without error

    println("variable declared with data type : " + a);
    println("variable declared without data type : " + b);
  }
}

Output

variable declared with data type : 21
variable declared without data type : 54

2) Immutable Variables

Variables that are made immutable are read-only variables in Scala. This means their value remains unchanged throughout the program. Scala variables are created using the val keyword. You can optionally give the data type of the variable with data type name with first letter capital.

Syntax to declare immutable variables

// Syntax with variable's data type 
val  variable_name : Data_type = value;

// Syntax without variable's data type
val  variable_name = value;

Scala example of immutable variables

object MyClass {
  def main(args: Array[String]): Unit = {
    val a: Int = 34;
    val b = 54;
    // a = 21; // Error : It won't work
    println("immutable variable declared with data type : " + a);
    println("immutable variable declared without datatype : " + b);
  }
}

Output

immutable variable declared with data type : 34
immutable variable declared without datatype : 54

3) Lazy initialization of Variables

Lazy initialization of variables are those variables that are calculated when the first time they are accessed. In scala mutable variables cannot be lazy.

Only val i.e. immutable variable can make lazy. This means these variables are calculated only once.

Syntax for lazy initialization of variables

// Syntax with variable's data type
lazy val  variable_name : Data_type = value;
    
// Syntax without variable's data type
lazy val  variable_name = value;

Scala code for lazy initialization of variables

object MyClass {
  def main(args: Array[String]): Unit = {
    lazy val a: Int = 34;
    val b = 54;

    // a++; this is not allowed in this case

    println(
      "immutable variable declared with data type with lazy declaration : " + a
    );
    println("immutable variable declared without datatype : " + b);
  }
}

Output

immutable variable declared with data type with lazy declaration : 34
immutable variable declared without datatype : 54

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.