Saturday, November 9, 2019

Difference Betwixt Var, Val, Too Def Inwards Scala

This is 1 of the oftentimes asked questions from Scala interviews. Even though both var as well as val keyword is used to declare variables inwards Scala at that topographic point is a subtle departure betwixt them. H5N1 var is a variable. It’s a mutable reference to a value. Since it’s mutable, its value may alter through the programme lifetime. On the other hand, val keyword represents a value. It’s an immutable reference, pregnant that its value never changes. Once assigned it volition e'er proceed the same value. It’s like to a in conclusion variable inwards Java or constants inwards other languages. It's besides worth to recollect that the variable type cannot alter inwards Scala. You may tell that a var behaves similarly to Java variables.

While the def is a constituent declaration. It is evaluated on call, like to Python, where def is besides used to declare a function. Let's encounter around code instance of var as well as val inwards Scala to sympathise the departure inwards to a greater extent than detail.


Difference betwixt var, val, as well as def inwards Scala

Here are around code snippets to present yous how precisely var, val as well as def are used inwards Scala programming linguistic communication to declare variables, values, as well as functions.

Here is code instance of val plant inwards Scala:

var x = 10


After this declare var x becomes a variable of type Int. You tin sack re-assign it a value e.g.

x = 14

Above should last fine as well as accepted past times Scala compiler because fourteen is besides of type Int just below volition non piece of employment because "four" is non Int as well as yous cannot alter the type of var inwards Scala.

x = "four" // non accepted past times the scala compiler 

Now, let's encounter around code instance of how to utilisation val keyword inwards Scala

val y = 13

After this assignment y becomes an Int variable just it's constant which agency yous cannot assign around other value to variable y, that's why it's known every bit value instead of a variable. You tin sack read to a greater extent than most Scala type organisation inwards "Programming inwards Scala" mass past times Martin Oderskey, the writer of Scala programming language.

Below, reassignment volition hit an fault 'error: reassignment to val'


y = 14

Now, let's encounter how yous tin sack utilisation the def keyword to declare functions inwards Scala:

def hello(name: String) = "Hello : " + name

This is a constituent which takes a String as well as prints String

hello("OOP") // "Hello : OOP" hello("FP") //  "Hello : FP"


Scala besides supports lazy evaluation of val, besides known every bit lazy val. It’s like to a val, just its value is alone computed when needed as well as it is defined using the lazy keyword. It’s especially useful to avoid heavy computations e.g. past times using short-circuit && as well as || logical variables.


Suppose yous receive got 2 val every bit shown below, 1 is lazy val as well as other is a normal one.

lazy val x = {   println("calculating value of x")   13 }  val y = {   println("calculating value of y")   20 }

Now, if yous perform the next operation

y + y 

Then the x was even so non last evaluated i.e. "calculating the values of x" volition non last printed because yous don't demand the value of x yet.

x + x

Now, yous demand the value of x thence the value of x volition last calculated as well as yous volition encounter the message "calculating the value of x" just alone once.

Here is the overnice summary of the departure betwixt val as well as var Scala variables:

 This is 1 of the oftentimes asked questions from  Difference betwixt var, val, as well as def inwards Scala



That's all most difference betwixt var, val, as well as def inwards Scala. In short, the val as well as var are evaluated when defined, piece def is evaluated on call. Also, val defines a constant, a fixed value which cannot last modified 1 time declared as well as assigned piece var defines a variable, which tin sack last modified or reassigned.  If yous are learning Scala, as well as then I besides propose reading a expert mass on Scala e.g. "Scala for the Impatient" by Cay S. Horstmann to fill upwards the gaps as well as acquire a expert agreement of fundamentals.

Further Learning
Scala: Getting Started
Learn By Example: Scala
Rock the JVM! Scala as well as Functional Programming for Beginners


No comments:

Post a Comment