Difference betwixt var, val, together with def inward Scala

This is 1 of the often asked questions from Scala interviews. Even though both var too val keyword is used to declare variables inwards Scala in that place is a subtle departure betwixt them. Influenza A virus subtype 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, important that its value never changes. Once assigned it volition ever choke along the same value. It’s like to a lastly variable inwards Java or constants inwards other languages. It's equally good worth to shout out upward that the variable type cannot alter inwards Scala. You may tell that a var behaves similarly to Java variables.

While the def is a component declaration. It is evaluated on call, like to Python, where def is equally good used to declare a function. Let's meet unopen to code event of var too val inwards Scala to sympathise the departure inwards to a greater extent than detail.


Difference betwixt var, val, too def inwards Scala

Here are unopen to code snippets to present you lot how precisely var, val too def are used inwards Scala programming linguistic communication to declare variables, values, too functions.

Here is code event of val plant inwards Scala:

var x = 10


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

x = 14

Above should live fine too accepted yesteryear Scala compiler because xiv is equally good of type Int just below volition non piece of occupation because "four" is non Int too you lot cannot alter the type of var inwards Scala.

x = "four" // non accepted yesteryear the scala compiler 

Now, let's meet unopen to code event of how to exercise val keyword inwards Scala

val y = 13

After this assignment y becomes an Int variable just it's constant which agency you lot cannot assign unopen to other value to variable y, that's why it's known equally value instead of a variable. You tin read to a greater extent than close Scala type organization inwards "Programming inwards Scala" majority yesteryear Martin Oderskey, the writer of Scala programming language.

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


y = 14

Now, let's meet how you lot tin exercise the def keyword to declare functions inwards Scala:

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

This is a component which takes a String too prints String

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


Scala equally good supports lazy evaluation of val, equally good known equally lazy val. It’s like to a val, just its value is exclusively computed when needed too it is defined using the lazy keyword. It’s especially useful to avoid heavy computations e.g. yesteryear using short-circuit && too || logical variables.


Suppose you lot receive got 2 val equally shown below, 1 is lazy val too other is a normal one.

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

Now, if you lot perform the next operation

y + y 

Then the x was notwithstanding non live evaluated i.e. "calculating the values of x" volition non live printed because you lot don't demand the value of x yet.

x + x

Now, you lot demand the value of x thence the value of x volition live calculated too you lot volition meet the message "calculating the value of x" just exclusively once.

Here is the prissy summary of the departure betwixt val too var Scala variables:

 This is 1 of the often asked questions from  Difference betwixt var, val, too def inwards Scala



That's all close difference betwixt var, val, too def inwards Scala. In short, the val too var are evaluated when defined, spell def is evaluated on call. Also, val defines a constant, a fixed value which cannot live modified 1 time declared too assigned spell var defines a variable, which tin live modified or reassigned.  If you lot are learning Scala, so I equally good advise reading a proficient majority on Scala e.g. "Scala for the Impatient" by Cay S. Horstmann to fill upward the gaps too acquire a proficient agreement of fundamentals.


Subscribe to receive free email updates:

0 Response to "Difference betwixt var, val, together with def inward Scala"

Posting Komentar