How a language treats variables and memory underlies endless interview questions. The key axes: static vs dynamic typing (must you declare a type?), primitive vs object (is a value stored directly or via a reference?), and where data lives (stack vs heap). C and Java are statically typed; Python is dynamic. Java splits primitives from objects; in Python, everything is an object.
Static typing (C, Java): a variable's type is fixed at compile time and declared. Dynamic typing (Python): a name is just a label bound to an object, and can be rebound to a different type. In C/Java, primitives (int, char) hold the value directly; objects hold a reference. In Python, a variable is always a reference to an object.
Typing across the three
| C | Java | Python | |
|---|---|---|---|
| Declare type? | yes (int x;) | yes (int x;) | no (x = 5) |
| Checked when | compile time | compile time | runtime |
| int size | platform (often 32-bit) | fixed 32-bit | arbitrary precision |
| Everything an object? | no | no (primitives aren't) | yes |
/* C */ int x = 5; // fixed type, value stored directly // Java int x = 5; // primitive; Integer x = 5; is an object # Python x = 5 # name bound to an int object; x = 'hi' is fine later
- In Python a variable is a name bound to an object, not a box holding a value. Assignment rebinds the name; it doesn't copy. That's why two names can refer to the same list and both 'see' a change.
- C and Java integers have a fixed size and can overflow; a 32-bit int wraps around past ~2.1 billion. Python integers have arbitrary precision and never overflow — they grow as needed.
- A primitive (int, char, boolean, double...) stores its value directly and isn't a class instance.
- An object (String, Integer, any class) is accessed via a reference to memory on the heap.
- Java auto-converts between them where needed — autoboxing (int→Integer) and unboxing (Integer→int).
- A C int has a fixed width (commonly 32 bits), so it can only hold a limited range of values.
- Exceeding that range wraps around (overflow), often to a negative number.
- Python integers are arbitrary-precision — the interpreter grows them as needed — so they never overflow.
- Static vs dynamic typing: C/Java check types at compile time; Python at runtime — a type error in Python only appears when that line runs.
- Fixed-size integer overflow in C/Java is a classic bug; Python is immune.
- In Python, assignment binds a name to an object and doesn't copy — crucial for mutability questions (next chapters).