Home » Scala language

val keyword with example in Scala

Scala val keyword usage and syntax: Here, we are going to learn about the val keyword in Scala, how to use val keyword in a Scala program? Explained example to use Scala val keyword.
Submitted by Shivang Yadav, on June 16, 2019

Scala val keyword

Scala val keyword is used to define a constant or controlled variable in the program. You can skip the declaration of data type in Scala while using val keyword. The variables declared using val keyword are immutable variables.

Immutable Variables

Immutable Variables are those variables whose value can not be changed in the program. These variable have a fixed value that remains constant throughout the program. Any attempt to change the value causes an error. Example of immutable variables, names, id, etc that are fixed. Usually, we tend to fix strings immutable so that important contained cannot be changed.

Syntax of immutable variable declaration using val keyword:

    val I = 102;
    val H : string = "IncludeHelp";

Syntax Explanation:

The val keyword is used to initialize the variables. The first initialization is of an int data type which is not declared explicitly, the compiler will take it based on its value. In the second initialization, the data type is explicitly given. This means that irrespective of the value the data type will be a string.

val keyword can define constant variables of all data type. You can not change the value of this variable. Once the value is initialized it is the final value.

Scala program to demonstrate example of val keyword

// Program that displays the usage of 
// val keyword in Scala

object ValKeyword {
	def main(args: Array[String]) {
		//Variable Initialized with value52
		val myVar = 52;
		val name : String = "IncludeHelp"
		
		print("Value of my myVar = " + myVar + "\n")
		print("Hello this is " + name)
	}
}

Output

Value of my myVar = 52
Hello this is IncludeHelp

Example explanation:

The val keyword here initializes two variables. myVar with value 52 which will itself be initialized as an integer. Another one is name defined as a string.

What if you change the value of val variable. In this code, we have changed the value of myVar.

// Program that displays the 
// usage of var keyword in Scala

object ValKeyword {
	def main(args: Array[String]) {
		//Variable Initialized with value52
		val myVar = 52;
		val name :String = "IncludeHelp"
		
		print("Value of my myVar = " + myVar + "\n")
		print("Hello this is " + name)
		myVar = 23;
		print("Value of my myVar = " + myVar + "\n")
	}
}

Output

ValKeyword.scala:12: error: reassignment to val
		myVar = 23;
                      ^
one error found
cat: /release: No such file or directory

Example explanation:

Changing the value of val variable is an error. The compiler outputs an error "reassignment of val" if you by mistake change its value.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.