Home » Scala language

Variables in Scala

Scala variables: Variables are a reference to a memory location where data is stored. In Scala, there are two types of variables based on the mutability. In this Scala tutorial, we will learn about Scala variables their usage and example code to implement them.
Submitted by Shivang Yadav, on July 15, 2019

Scala variables

A variable is named a reference to a memory location. 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.

Scala variables are memory locations. 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 with variable's data type
    var  variable_name : Data_type = value;

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

Example:

object MyClass {
    def main(args: Array[String]) {
        var a : Int = 33; 
        var b = 54;
        
        a++;
        
        println("variable declared with data type : " + a );
        println("variable declared without data type : " + b );
    }
}

Output

variable declared with data type : 34
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 with variable's data type 
    val  variable_name : Data_type = value;

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

Example:

object MyClass {
    def main(args: Array[String]) {
        val a : Int = 34; 
        val b = 54;
        // a++; this is not allowed in this case
        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 with variable's data type
    lazy val  variable_name : Data_type = value;
    
    //Syntax without variable's data type
    lazy val  variable_name = value;

Example:

object MyClass {
    def main(args: Array[String]) {
        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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.