Variables & Data types

In Scala, variables can be declared in two ways: using val or var, depending on whether the value is immutable or mutable. Here’s an explanation of each along with examples.

val: Immutable Variables

  • val is used to declare immutable variables. Once a value is assigned to a val, it cannot be changed (similar to a constant or final variable in other languages like Java).
  • Immutable means that the reference to the value cannot be changed, but the value itself (if it’s mutable) can still be modified.

Syntax:

val variableName: DataType = value

Example :

val name: String = "Alice"
val age: Int = 25
val pi: Double = 3.14

// The following will cause an error because `val` is immutable.
// name = "Bob" // Error: reassignment to val

Advantages of val: Promotes immutability, leading to safer and more predictable code, especially in concurrent environments.

var: Mutable Variables

  • var is used to declare mutable variables. This means the value can be changed after the initial assignment.
  • Mutable means that both the reference and the value can be changed.
var variableName: DataType = value

Example :

var counter: Int = 0
counter = 1 // Allowed because `var` is mutable
counter = counter + 1 // Now counter is 2
  • When to use var: Use var sparingly, as too much mutability can make code harder to maintain, debug, and reason about.

Type Inference:

Scala supports type inference, which means that the compiler can automatically deduce the type of a variable based on the value assigned to it. You don’t need to explicitly specify the type, though you can if you want to.

Example:

val name = "Alice"  // Compiler infers type as String
var count = 10 // Compiler infers type as Int

You can still explicitly declare types for clarity if you prefer:

val name: String = "Alice"
var count: Int = 10

Variable Scope:

  • Variables in Scala follow block scope. They are only accessible within the block in which they are defined.
  • Variables defined inside a method or a block cannot be accessed outside of that block.

Example:

def myMethod(): Unit = {
val x = 10
println(x) // Valid, x is accessible here
}
// println(x) // Error: x is not accessible outside the method

Lazy Initialization (lazy keyword):

Scala also supports lazy initialization using the lazy keyword. A lazy val is not initialized until it is accessed for the first time.

Example:

lazy val greeting = "Hello, world!"
// `greeting` will only be initialized when it is used for the first time
println(greeting)

Difference Between val and var:

Featurevalvar
MutabilityImmutable (cannot be reassigned)Mutable (can be reassigned)
UsagePreferred for immutabilityUsed when reassignment is needed
Thread SafetyMore thread-safe due to immutabilityLess thread-safe due to mutability
Exampleval age = 30var age = 30; age = 35

Example:

// val example
val name: String = "John"
println(name) // Prints: John
// name = "Jane" // Error: reassignment to val

// var example
var score: Int = 100
println(score) // Prints: 100
score = 150 // Reassigning new value
println(score) // Prints: 150

Summary:

  • val: Immutable variable, its value cannot be reassigned.
  • var: Mutable variable, its value can be reassigned.
  • Scala emphasizes immutability as it is safer for concurrent programming and makes code more predictable.
  • Use val wherever possible and var only when necessary.

Data types

Scala provides a variety of data types to represent different kinds of values, such as numbers, characters, strings, and more. These data types are based on the underlying Java types but come with additional features specific to Scala.

Type Hierarchy in Scala:

Any
├── AnyVal
│ ├── Byte
│ ├── Short
│ ├── Int
│ ├── Long
│ ├── Float
│ ├── Double
│ ├── Char
│ ├── Boolean
│ └── Unit
└── AnyRef
├── String
├── List
└── Other Reference Types
└── Null (special type)

Type Inference in Scala:

Scala can automatically infer the type of a variable based on the assigned value, so you don’t always need to explicitly specify the type.

Example:

val inferredInt = 10         // Type is inferred as Int
val inferredDouble = 3.14 // Type is inferred as Double
val inferredString = "Scala!" // Type is inferred as String

Basic Data Types:

Data TypeDescriptionExample
Byte8-bit signed integer (-128 to 127)val a: Byte = 100
Short16-bit signed integer (-32,768 to 32,767)val b: Short = 32000
Int32-bit signed integerval c: Int = 100000
Long64-bit signed integerval d: Long = 10000000000L
Float32-bit floating-point numberval e: Float = 3.14f
Double64-bit floating-point number (default for decimals)val f: Double = 3.14159
Char16-bit Unicode characterval g: Char = 'A'
StringA sequence of charactersval h: String = "Hello, Scala!"
BooleanRepresents true or falseval i: Boolean = true
UnitRepresents no value (like void in Java)def printMsg(): Unit = println("Message")
NullType of the null literalval j: String = null
NothingThe subtype of all types, represents no value (used for exceptions)Cannot be directly instantiated
AnyThe supertype of all typesval k: Any = "Can be anything"
AnyRefThe supertype of all reference types (equivalent to Java’s Object)val obj: AnyRef = new Object()

Numeric Data Types:

Example of Basic Numeric Data Types:

val byteVal: Byte = 127
val shortVal: Short = 32767
val intVal: Int = 100000
val longVal: Long = 1000000000L
val floatVal: Float = 3.14f
val doubleVal: Double = 3.141592653589793
  • Byte, Short, Int, and Long are used for whole numbers of various sizes.
  • Float and Double are used for floating-point numbers, where Double is more precise.
  • You must append an L to indicate a Long and an f to indicate a Float.

Arithmetic Operations Example:

val sum: Int = 10 + 20
val product: Double = 10.5 * 2
val quotient: Float = 15.0f / 4.0f
println(s"Sum: $sum, Product: $product, Quotient: $quotient")

Character and String Data Types:

  • Char represents a single Unicode character, and it is enclosed in single quotes (').
  • String is a sequence of characters enclosed in double quotes (").

Example :

val charVal: Char = 'A'
val stringVal: String = "Hello, Scala!"
println(s"Char: $charVal, String: $stringVal")

Boolean Data Type:

  • Boolean represents a truth value: either true or false.
val isScalaFun: Boolean = true
println(s"Is Scala fun? $isScalaFun")

Unit, Null, Nothing:

  • Unit: Similar to void in other languages, it represents the absence of a value. It is the return type of methods that do not return anything.
  • Null: Used to represent the null reference, mainly for reference types like String. It cannot be assigned to value types like Int.
  • Nothing: Represents the absence of a value, typically used for functions that never return (e.g., throw exceptions).
def sayHello(): Unit = {
println("Hello, World!")
}

val nullStr: String = null
// val nullInt: Int = null // This will cause an error, since primitive types can't be null

Any, AnyVal, AnyRef:

  • Any: The root of Scala’s type hierarchy. Every type in Scala is a subtype of Any.
  • AnyVal: Represents value types such as Int, Double, Boolean, etc.
  • AnyRef: Represents reference types (like objects), similar to Java’s Object.

Example :

val anyVal: Any = "I can be any type"
val anyRef: AnyRef = "I am a reference type"
println(s"Any: $anyVal, AnyRef: $anyRef")

Summary

Data TypeDescriptionExample
Byte8-bit signed integerval a: Byte = 127
Short16-bit signed integerval b: Short = 32767
Int32-bit signed integerval c: Int = 100000
Long64-bit signed integerval d: Long = 1000000000L
Float32-bit floating-pointval e: Float = 3.14f
Double64-bit floating-pointval f: Double = 3.1415926535
CharA single 16-bit characterval g: Char = 'A'
StringA sequence of charactersval h: String = "Hello, Scala!"
BooleanA boolean value (true or false)val i: Boolean = true
UnitRepresents no value (like void)def printMsg(): Unit = println("")
NullA type representing the null referenceval j: String = null
NothingA type for non-returning methods (e.g., throwing exceptions)throw new Exception
AnySupertype of all typesval k: Any = 42
AnyValSupertype of all value typesval l: AnyVal = 5.0
AnyRefSupertype of all reference typesval m: AnyRef = "reference"