Skip to content

Variable Declaration

Variable Declaration#

In computer science, variables are like named containers for storing data. Your program uses variables to hold numbers, text, and other information so it can use them later.

Variables can be mutable (changeable) or immutable (unchangeable). Mutable variables can be reassigned new values, while immutable variables keep their initial value. This affects how the program manages memory and ensures code predictability.

graph TD
    A[Variable Declaration] --> B{Mutability Choice}
    B --> C[Mutable Variables]
    B --> D[Immutable Variables]
    C --> E[Can be reassigned]
    D --> F[Cannot be reassigned]
    E --> G[Flexible but riskier]
    F --> H[Safer and more predictable]

    style C fill:#ffcdd2
    style D fill:#c8e6c9
    style G fill:#ffcdd2
    style H fill:#c8e6c9

Mutable Variables#

Mutable variables can be changed after they are created. They are useful for counters, user input, and any data that needs to be updated during program execution.

var name = "Fadi";
name = "William"; // Can be reassigned
System.out.println("Hello, " + name + "!");
// Output: Hello, William!
var name = "Fadi"
name = "William" // Can be reassigned
println("Hello, $name!")
// Output: Hello, William!
let myName = "Fadi";
myName = "William"; // Can be reassigned
console.log(`Hello, ${myName}!`);
// Output: Hello, William!
var name = "Fadi";
name = "William"; // Can be reassigned
print("Hello, $name!");
// Output: Hello, William!
var name = "Fadi"
name = "William" // Can be reassigned
print("Hello, \(name)!")
// Output: Hello, William!
name = "Fadi"
name = "William" # Can be reassigned
print(f'Hello, {name}!')
# Output: Hello, William!

Immutable Variables#

Immutable variables cannot be changed after they are created. They are safer to use and help prevent bugs by ensuring values stay constant.

Why immutable variables are better for multi-threading:

  • Thread Safety: Multiple threads can safely read the same immutable data without synchronization
  • No Race Conditions: Since values can't change, there's no risk of one thread modifying data while another is reading it
  • Simpler Code: No need for locks, mutexes, or other synchronization mechanisms
  • Better Performance: Compilers can optimize immutable data more aggressively
final var name = "Fadi";
// name = "William"; // Error: cannot reassign
System.out.println("Hello, " + name + "!");
// Output: Hello, Fadi!
val name = "Fadi"
// name = "William" // Error: cannot reassign
println("Hello, $name!")
// Output: Hello, Fadi!
const myName = "Fadi";
// myName = "William"; // Error: cannot reassign
console.log(`Hello, ${myName}!`);
// Output: Hello, Fadi!
const name = "Fadi";
// name = "William"; // Error: cannot reassign
print("Hello, $name!");
// Output: Hello, Fadi!
let name = "Fadi"
// name = "William" // Error: cannot reassign
print("Hello, \(name)!")
// Output: Hello, Fadi!

Python doesn't have true immutable variables. All variables can be reassigned.

Multiple Variable Declaration#

You can declare multiple variables in one line.

String guitarString = "D'addario" , guitarPick = "Dunlop", guitarBridge = "Floyd Rose";

Note that the variables should have the same type.

var guitarString = "D'addario"; var guitarPick = "Dunlop"; var guitarBridge = "Floyd Rose"
let guitarString = "D'addario" , guitarPick = "Dunlop", guitarBridge = "Floyd Rose";
var guitarString = "D'addario", guitarPick = "Dunlop", guitarBridge = "Floyd Rose";
var guitarString = "D'addario", guitarPick = "Dunlop", guitarBridge = "Floyd Rose"
guitarString, guitarPick, guitarBridge = "Floyd Rose", "D'addario", "Dunlop"