Scala: Difference between var and val.

I started learning scala some couple of days back. There is this good book Beginning Scala by Brian M. Clapper (http://www.amazon.com/review/R2JJXX1B3PRUW8). I stared with simple programs running them in scala command line interpreter.
There are two keywords in scala to declare a variable, val and var.
The difference between val and var. var much like java delcaration. But val is little different. Once we declared a variable using val we can not change the reference to point to another reference. We can assume it like java final keyword. Let me show this by an example:

I started learning scala some couple of days back. There is this good book Beginning Scala by Brian M. Clapper (http://www.amazon.com/review/R2JJXX1B3PRUW8). I stared with simple programs running them in scala command line interpreter.

There are two keywords in scala to declare a variable, val and var.
The difference between val and var. var much like java delcaration. But val is little different. Once we declared a variable using val we can not change the reference to point to another reference. We can assume it like java final keyword. Let me show this by an example:

scala> val a:Int = 20
a: Int = 20

Now change a to point to another value.

scala> a = 40
<console>:5: error: reassignment to val
a = 40
^

Here we get error saying that we can not assign the val to another value. But we can define another variable with same name.

scala> val a:Int = 40
a: Int = 40

But we can not do this in scala file, and we get  error.

scala> def vivek() ={
| val a:Int = 20
| val a:Int = 30
| println(a)
| }
<console>:7: error: a is already defined as value a
val a:Int = 30
^

This is because we are trying to define two variable with same name in same scope.

scala> def vivek() ={
|  if(true){
|   val a:Int = 20
|    println(a)
|  }
| val a:Int = 30
| println(a)
| }
vivek: ()Unit
scala> vivek
20
30

Now consider var. var is like any other normal java variable declaration.

scala> var a:Int = 30
a: Int = 30
scala> println(a)
30
scala> a = 40
a: Int = 40
scala> println(a)
40

Advertisements

6 responses to “Scala: Difference between var and val.

  1. Wow. My first Scala question and you answered it nicely!

  2. Hello There. I discovered your weblog the use of msn.
    That is a really smartly written article. I will make sure to bookmark it and return to read
    more of your useful information. Thank you for
    the post. I will definitely return.

  3. http://petdogcouponcode.wordpress.com

    Very good information. Lucky me I came across your blog by
    accident (stumbleupon). I’ve bookmarked it for later!

  4. Greetings from Los angeles! I’m bored to death at work so I decided to browse your blog on my iphone during lunch break. I love the information you present here and can’t wait to take a look when
    I get home. I’m shocked at how quick your blog loaded on my cell phone .. I’m not
    even using WIFI, just 3G .. Anyhow, superb blog!

  5. WOW just what I was looking for. Came here by searching
    for Sharp Shades Coupon Code

  6. Thank you. Useful informatiom

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s