Variables, Constants & Mutability
Interactive multi-language comparison: C, JavaScript, Python, and Java.
SLIDE 1 / 6
1. Memory, Identifiers & Values
Every variable or constant is a RAM memory location storing a value under a named identifier.
Identifier: The symbolic name in code (e.g. score).
Address: Physical RAM location (e.g. 0x7ffd9).
Value: The binary data stored inside.
SIMULATED RAM VISUALIZER
0x7FFD01 score (int) 100
0x7FFD05 MAX_LIMIT (const) 999 🔒
0x7FFD09 user_name (string) "Facundo"
2. C: Static Typing & Direct Memory
In C, exact data types are declared before use. The compiler allocates fixed byte space.
- int / float: Modifiable variables anytime.
- const: Compiler instruction forbidding reassignment.
main.c
#include <stdio.h>
int main() {
int contador = 0; // Variable mutable
const float PI = 3.14159f; // Constante inmutable
contador = 10; // OK: Cambia valor
// PI = 3.0; // Error de compilación GCC
return 0;
} 3. JavaScript: var, let, const & References
JS uses dynamic typing. const prevents variable reassignment, but DOES NOT freeze object properties.
let: Block-scoped variable.
const (Primitivo): Completely immutable.
const (Objeto): Reference is fixed, but object properties can mutate.
app.js
let edad = 25;
edad = 26; // OK
const config = { tema: 'oscuro' };
config.tema = 'claro'; // ¡Mutación permitida!
// config = {}; // TypeError en V8
Object.freeze(config); // Inmutabilidad real 4. Python: Names as Object References
In Python everything is an object. Variables are labels referencing objects in the Heap.
- Immutables: int, float, str, tuple (create new object on change).
- Mutables: list, dict, set (modify in-place).
- typing.Final: Hint for static checkers (Mypy).
script.py
from typing import Final
MAX_USERS: Final[int] = 500
numeros = [1, 2, 3]
numeros.append(4) # Mutación in-place en Heap
texto = "Hola"
# texto[0] = "h" # TypeError: str es inmutable 5. Java: Primitives, Objects & final
Java strictly distinguishes primitive types (stored by value) and objects (stored by heap reference).
final: Prevents variable or pointer reassignment.
String: Immutable class by design (String Pool).
Main.java
public class Main {
public static void main(String[] args) {
final double IVA = 0.21;
// IVA = 0.15; // Error de compilador javac
final StringBuilder sb = new StringBuilder("Hi");
sb.append(" World"); // OK: Contenido muta
}
} 6. Live Mutation Simulator
Select a language and trigger an invalid mutation:
C / GCC Compiler
const float PI = 3.14159f;
PI = 3.0f; // Intentando reasignar...
COMPILER ERROR
main.c:6:5: error: assignment of read-only variable 'PI'
Navigation: Left / Right Arrows