The following is an example program that explains the process of variable declaration in Scala. This program declares four variables — two variables are defined with type declaration and the remaining two are without type declaration.
Example
object Demo {
def main(args: Array[String]) {
var myVar :Int = 10;
val myVal :String = "Hello Scala with datatype declaration.";
var myVar1 = 20;
val myVal1 = "Hello Scala new without datatype declaration.";
println(myVar); println(myVal); println(myVar1);
println(myVal1);
}
}
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
\>scalac Demo.scala
\>scala Demo
Output
10
Hello Scala with datatype declaration.
20
Hello Scala without datatype declaration.