Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, variables are storage locations that can hold values. There are two primary types of variables in Scala:
val
: Immutable (read-only) variables.var
: Mutable variables.val
(Immutable Variables):When you declare a variable using val
, it becomes immutable. This means once you assign a value to a val
variable, you cannot change it.
val x = 5 // x = 10 // This will result in a compilation error
Immutable variables are thread-safe and are a fundamental concept in functional programming, which Scala encourages. When you use val
, you're ensuring that no one can alter the value unintentionally.
var
(Mutable Variables):Variables declared with var
are mutable and can be reassigned.
var y = 10 y = 15 // This is allowed
However, mutable state can lead to more complex reasoning, especially in concurrent applications. Thus, it's a good practice to prefer val
over var
whenever possible.
Scala has a strong, static type system, and in many cases, it can infer the type of a variable. But you can also specify the type if you wish:
val a: Int = 10 var b: String = "Hello"
In Scala, you can also declare variables as lazy
. A lazy
variable is not initialized when it's defined. Instead, its initialization is deferred until the variable is accessed for the first time:
lazy val z = { println("Initializing z") 50 } println("Before accessing z") val result = z // At this point, "Initializing z" will be printed
lazy
can be useful for expensive computations or resources that should only be initialized when they're needed.
Variable scope in Scala determines the accessibility of a variable. Local variables (defined inside a method or block) have local scope and aren't accessible outside the method/block. Variables defined at the object or class level have a wider scope and can be accessed by any method within that object or class.
val
over var
: Using immutable variables can help make your code more readable, maintainable, and less error-prone.In summary, Scala provides flexible mechanisms for variable declaration, catering both to functional programming paradigms (with val
) and more traditional imperative styles (with var
). The language's design encourages developers to think about the mutability and scope of their variables, leading to cleaner and safer code.
Declaring variables in Scala:
val
or var
keyword followed by the variable name and optional type annotation.// Declaring variables val immutableVariable: Int = 42 var mutableVariable: String = "Hello"
Val vs Var in Scala:
val
declares an immutable variable, while var
declares a mutable variable. The value of a val
cannot be reassigned.// Val vs Var val immutableValue: Int = 42 var mutableValue: String = "Hello" // Error: Cannot reassign to a val // immutableValue = 50 // OK: Can reassign to a var mutableValue = "World"
Mutable and immutable variables in Scala:
val
declares immutable variables, and var
declares mutable variables. Immutable variables cannot be reassigned after initialization.// Mutable and immutable variables val immutableValue: Int = 42 var mutableValue: String = "Hello" // Error: Cannot reassign to a val // immutableValue = 50 // OK: Can reassign to a var mutableValue = "World"
Scope of variables in Scala:
// Scope of variables def exampleMethod(): Unit = { val localVar: Int = 10 // localVar is accessible only within this method } // Error: localVar is not accessible here // println(localVar)
Global variables in Scala:
// Global variables object GlobalExample { val globalVar: String = "I'm global" def printGlobalVar(): Unit = { println(globalVar) } } // Accessing globalVar GlobalExample.printGlobalVar()
Lazy variables in Scala:
lazy
keyword.// Lazy variables lazy val lazyValue: Int = { println("Initializing lazyValue") 42 } // lazyValue is not initialized until it is accessed println("Before accessing lazyValue") println(lazyValue)
Pattern matching with variables in Scala:
// Pattern matching with variables val value: Any = 42 value match { case 0 => println("Zero") case x if x > 0 => println(s"Positive: $x") case _ => println("Negative or other") }
Shadowing variables in Scala:
// Shadowing variables val outerVar: Int = 10 def exampleMethod(): Unit = { val outerVar: Int = 20 // The outerVar inside this method shadows the outerVar in the outer scope println(s"Inner scope: $outerVar") } exampleMethod() println(s"Outer scope: $outerVar")