How is memory allocated for a variable declaration in Python vs. Java? Without a compilation step how does an interpreted language know how much memory is needed for a variable?
-
6look up strong typing vs weak typing.– TTTApr 15, 2014 at 17:35
-
3@rpg711 Variable declaration can be mandatory in weakly typed languages too. And even when you might see it as not mandatory like in JavaScript, declaring a variable can set its scope.– Denys SéguretApr 15, 2014 at 17:36
-
4It's decoupled from interpreted vs. compiled as well as typing rules. There are no restrictions preventing someone from creating a compiled language with strong typing that doesn't require variable declarations. Purely a design decision on the part of the language creators.– Max WallaceApr 15, 2014 at 17:38
-
2@rpg711 In JavaScript a variable you use without declaring it is in the global scope, not "where you first use it" (which is a common source of bugs).– Denys SéguretApr 15, 2014 at 17:47
-
3@deathlee It's not a stupid question. It's a little broad though, and you weren't very clear. And the discussion derailed a bit :p It seems as if no one here has a definitive answer.– keyserApr 15, 2014 at 17:57
1 Answer
Before being usable, variables must be allocated a memory location and then initialized--whether in Java, Python, or even Logo.
Declare means that you make that variable come to life with a specific snippet of code, with (using Java as an example) something like
int i;
Person p;
These are declared, but not initialized. They are now assigned a location in memory--which, in some languages, may be ever-changing, both in location and size. But regardless, there is now some physical location in memory that the runtime environment can query, to retrieve the variable (either an indirect pointer to it, or the actual location itself).
Now that it has an empty "box" in which to go, it must be filled, which is to say it must be "initialized":
i = 3;
p = new Person();
Now there is something concrete in the box. It is ready for use. Attempting to use it before its initialized will result (in Java) in a NullPointerException
.
Some languages require you to declare variables, in order to explicitly allocate memory for it (location and/or size). Some languages do this memory-allocation for you. As stated in the comments to both your question and this answer, there's a lot of variation.
-
I think someone misread this as you saying Python must have its variables declared, when you really meant that python must have its variables initialized (or else how would you use them?) Apr 15, 2014 at 17:42
-
1
-
Okay, so two downvotes so far, with no criticism on what the problem is. This is a pretty basic concept. I'd like to know what language does not do this. Apr 15, 2014 at 17:45
-
1
-
1Better, but that's still a highly nonstandard definition of "declare". Also, many languages don't assign variables to locations in memory. For example, in Python, it's fully possible for a global variable's dict entry to change location over the course of a program's execution. Apr 15, 2014 at 17:49