Scala Program To Create The Mutable Variable

In this post, we are going to see how you can create a mutable variable in scala programming. We are going to assign some value to a variable and then assign another value to the same variable. Let’s see what will be the output of the program.

Program 2: Scala Program To Create The Mutable Variable

object divineseo{  
    def main(args:Array[String]){  
        var num = 30
        num = 200  
        println ("Number: "+num) 

We have already seen the object, def main, and println. So we are not going for the explanation of the following. In case, you missed the go to our first post on the Scala program to print “Hello World”.

Here we declare a variable using the Keyword var i.e., num, and at the same time assign it the integer value 30. A variable never declares twice but we can assign the value as many times as we like. We again assign 200 to the num variable.


Number: 200

As you can see the num value has been replaced with the new value.

Leave a Comment