0
Sponsored Links


Ad by Google
The Scala stands for "Scalable Language" is a pure object oriented with fully supported functional programming language. Scala is concise and very much comparable with Java programming language.
The origin of Scala is mix of many languages like Java, C#, C++, Haskell etc.
Scala runs on JVM(Java Virtual Machine), Scala source code compiled into byte code similar to Java, you can easily mix Java code inside Scala or you can use Java libraries inside you Scala project. Although Scala is very much similar to Java but still it has some differences which makes it more demanding in a very short period of time unlike other languages. Here is detailed differences between Scala and Java Scala Vs Java

In this tutorial, I am going to show you What is Scala Variables and different types of Variables in Scala like var, val, lazy with example.
In Scala variables comes in three different variety-
  • var
  • val
  • lazy val
var
A variable with mutable state or you can say mutable variable defines using var keywords, which can be modified.
Syntax:
package com.javamakeuse.tutorial

object VariableExample {
  def main(args: Array[String]): Unit = {

    // defining state variable
    var state = "Open"

    // another way to defining variable
    var anotherWay: String = "Closed"

    // ;semicolon in scala is optional
    println("Semicolon is optional " + state)

    // printing variable values on console
    println("With semicolon; " + anotherWay);
  }
}
Output:
Semicolon is optional Open
With semicolon; Closed

Note: Semicolon(;) is optional in Scala, for Scala set-up, visit step by step guide to set-up scala.

val
A variable with immutable state, that means you cannot modifies the value once defines with val keywords. val defines a fixed value, which cannot be modifies.
Syntax:
package com.javamakeuse.tutorial

object VariableExample {
  def main(args: Array[String]): Unit = {

    // defining state variable
    val pi = 3.1415

    //reassignment to val will gives compile time error.
    //pi =12;

    // another way to defining variable
    var anotherWay: Double = 3.14

    // ;semicolon in scala is optional
    println("Semicolon is optional " + pi)

    // printing variable values on console
    println("With semicolon; " + anotherWay);
  }
}
Output:
Semicolon is optional 3.1415
With semicolon; 3.14

Note: reassignment of val variable will gives you compile time error, for overview of Scala read what is Scala.

lazy val
A variable which values are calculated on demand, only when they accessed for the first time, it's similar to val with pre lazy keyword. For more on lazy in scala, here is a separate post on lazy evaluation in scala.
Syntax:
package com.javamakeuse.tutorial

object VariableExample {
  def main(args: Array[String]): Unit = {

    // defining state variable
    lazy val pi = 3.1415

    //reassignment to val will gives compile time error.
    //pi =12;

    // ;semicolon in scala is optional
    println("Lazily evaluated " + pi)
  }
}
Output:
Lazily evaluated 3.1415

Note: lazy keyword cannot be used with var keyword. For hello world in scala visit Scala tutorial

Scope of Variables

Three different scope exist for variables-
  • Field variable
  • Local variable
  • Method parameter
Field variables: It can be defines by any of the above variables, and they belong to an Object and can be accessed by any method or anywhere depending on the accessibility used.

 Local variable: It can be defines inside method only and it can be accessed only inside the method it defined. It can be any of the above variable like var, val, lazy val

Method parameter: variables which values are passed as parameter of the method while calling, and it can be only accessible from inside the method, and only val can be defines as a method parameter.

Note: There are no static variable in Scala unlike Java, because in Scala everything is Object.
Sponsored Links

0 comments:

Post a Comment