MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Var Val and Def

Examples

var


A var is a reference variable, similar to variables in languages like Java. Different objects can be freely assigned to a var, so long as the given object has the same type that the var was declared with:

scala> var x = 1
x: Int = 1
scala> x = 2
x: Int = 2
scala> x = "foo bar"
<console>:12: error: type mismatch;
 found : String("foo bar")
 required: Int
 x = "foo bar"
 ^

Note in the example above the type of the var was inferred by the compiler given the first value assignment.

val


A val is a constant reference. Thus, a new object cannot be assigned to a val that has already been assigned.

scala> val y = 1
y: Int = 1
scala> y = 2
<console>:12: error: reassignment to val
 y = 2
 ^

However, the object that a val points to is not constant. That object may be modified:

scala> val arr = new Array[Int](2)
arr: Array[Int] = Array(0, 0)
scala> arr(0) = 1
scala> arr
res1: Array[Int] = Array(1, 0)

def


A def defines a method. A method cannot be re-assigned to.

scala> def z = 1
z: Int
scala> z = 2
<console>:12: error: value z_= is not a member of object $iw
 z = 2
 ^

In the above examples, val y and def z return the same value. However, a def is evaluated when it is called, whereas a val or var is evaluated when it is assigned. This can result in differing behavior when the definition has side effects:

scala> val a = {println("Hi"); 1}
Hi
a: Int = 1
scala> def b = {println("Hi"); 1}
b: Int
scala> a + 1
res2: Int = 2
scala> b + 1
Hi
res3: Int = 2


Conclusion

In this page (written and validated by ) you learned about Scala Var Val and Def . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala While Loops.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.