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. – TTT Apr 15 '14 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éguret Apr 15 '14 at 17:36
-
Might it have something to do with interpreted vs. compiled languages? – DerStrom8 Apr 15 '14 at 17:37
-
@dystroy but ultimately it's more or less just convention, because most/all of those languages have closures anyways – TTT Apr 15 '14 at 17:37
-
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 Wallace Apr 15 '14 at 17:38
-
@dystroy why, using a variable without declaring it sets the scope directly at where you first use it, same as declaring the variable, it makes no difference whether or declare it or just use it in a weakly typed language. you might end up with really confusing, hard to read code, but it is all the same functionally – TTT Apr 15 '14 at 17:45
-
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éguret Apr 15 '14 at 17:47
-
@dystroy well, in python I believe the variables are treated as local variables when values are assigned to them, this question is not about javascript (well, the tags aren't) : http://stackoverflow.com/questions/370357/python-variable-scope-question?rq=1 – TTT Apr 15 '14 at 17:50
-
It seems I asked a stupid question:-) But thx,I still figured something out from the comments. – hidemyname Apr 15 '14 at 17:54
-
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. – keyser Apr 15 '14 at 17:57
-
@ᴋᴇʏsᴇʀ i'll guess the only people that actually know are those that work on the interpreters themselves, most people don't need to concern themselves with the fine details of typing, we just accept strong typing and weak typing, there is no real benefit to going deeper and investigating how those variables are allocated because we're dealing with high-level languages where most of the time everything but type(and sometimes even type) are abstracted out of the syntax/language – TTT Apr 15 '14 at 18:10
-
@rpg711 Strong vs weak typing is not the answer to this question. Both Python and Java are considered strongly typed. See http://en.wikipedia.org/wiki/Python_%28programming_language%29#Typing and http://en.wikipedia.org/wiki/Java_%28programming_language%29. The difference between a strong vs weakly typed language is how hard it is to subvert the type system. For example, C is a weakly typed language because you can cast things to and from void * without the compiler knowing what you did. – Khaelex Apr 15 '14 at 18:35
1 Answers
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.

- 1
- 1

- 19,847
- 17
- 77
- 108
-
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?) – TankorSmash Apr 15 '14 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. – aliteralmind Apr 15 '14 at 17:45
-
1Isn't it the other way around (in e.g. C)? Declaring gives you memory, initializing puts stuff there. No downvote from me :p, but I'm [unsure](http://stackoverflow.com/a/11008311/645270) if your first sentence is true. It might just be that people frown upon you calling it _declaring_. – keyser Apr 15 '14 at 17:45
-
-
this is right... the python interpreter must have some concept of the variables it's dealing with or it would have issues performing operations on the variables – TTT Apr 15 '14 at 17:47
-
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. – user2357112 Apr 15 '14 at 17:49
-
@user2357112: I've updated it to incorporate your comment, but I don't think this level of accuracy is necessary to a newbie question such as this. – aliteralmind Apr 15 '14 at 17:51
-
I wouldn't call this a newbie question. It arises amongst beginners, sure, but it's tough to answer :p – keyser Apr 15 '14 at 17:59
-
My primary concern here is the definition of "declare". You're using it to refer to an entirely separate concept from the standard usage. Variable declarations are things like `int x;` or `var x;` or `Dim x As Integer`. It doesn't have anything to do with allocating storage for the variable. For example, if you declare an automatic variable `int x;` in C, that doesn't assign storage for the variable. The variable is allocated storage on the stack every time the function is entered, at runtime, and that storage may be at a different location every time. If you declare a VLA `int x[n]`... – user2357112 Apr 15 '14 at 18:05
-
-
@user2357112: Ah. I see. I'll change it from "declare" to "allocate a physical storage location". – aliteralmind Apr 15 '14 at 18:07
-
@ᴋᴇʏsᴇʀ: The question itself, about the concept of declare and initialize, is not a newbie question. [*This question*](http://stackoverflow.com/questions/23090669/why-do-we-need-to-declare-the-variable-before-we-use-it-in-some-languages-but-n) is. But you're right. I'm learning something myself here. – aliteralmind Apr 15 '14 at 18:14