Java Forum / General / November 2006
array intialization for primitives
Eric - 21 Nov 2006 21:54 GMT Hi, quick question. Suppose I do:
void dumbMethod (int i) { int[] x; x = new int[i]; }
Are the elements of x necessarily initialized to anything? I have noticed they are often 0, but I don't know if this is a guarantee.
Also, and this is unrelated, but does Java have a distinction (to the programmer) between memory on the stack and memory on the heap?
Robert Klemme - 21 Nov 2006 22:04 GMT > Hi, quick question. Suppose I do: > [quoted text clipped - 5 lines] > Are the elements of x necessarily initialized to anything? I have > noticed they are often 0, but I don't know if this is a guarantee. I believe they have to be initialized to 0 according to the JLS. You would have to check there for a definitive answer.
> Also, and this is unrelated, but does Java have a distinction (to the > programmer) between memory on the stack and memory on the heap? Well, the stack is the stack and the heap is the heap. Keep in mind that objects are always stored on the heap. Basically you only have variables and primitive type values on the stack.
robert
Eric - 21 Nov 2006 22:12 GMT > I believe they have to be initialized to 0 according to the JLS. You > would have to check there for a definitive answer. Thanks.
> Well, the stack is the stack and the heap is the heap. Keep in mind > that objects are always stored on the heap. Basically you only have > variables and primitive type values on the stack. > > robert That makes sense. So could a single object be on the stack? What about an object array of constant size? ArrayList[10] or something like that. Or when you say objects are always stored on the heap, do you really mean always?
-Eric
Robert Klemme - 21 Nov 2006 23:05 GMT >> I believe they have to be initialized to 0 according to the JLS. You >> would have to check there for a definitive answer. [quoted text clipped - 9 lines] > that. Or when you say objects are always stored on the heap, do you > really mean always? I am not the JLS, but I am a Computer Scientist and this is a technical forum. Now estimate the chance that I mean always when I say "always"...
:-) robert
Oliver Wong - 21 Nov 2006 23:44 GMT >> Well, the stack is the stack and the heap is the heap. Keep in mind that >> objects are always stored on the heap. Basically you only have variables [quoted text clipped - 6 lines] > when you say objects are always stored on the heap, do you really mean > always? Usually people who ask whether an object is "on the stack" or "on the heap" have a C background, and think that an object being on the stack or being on the heap is meaningful with respect to the semantics of the program. Usually people are more interested in these semantics than whether the object is actually on the stack or on the heap. In other words, they're asking the wrong question. I don't know if that's the case for you, but just in case it is, you might want to rethink what it is you're really trying to find out about Java's behaviour.
FWIW, some JVMs (I think IBM's JVM does this) perform analysis on the program and make their own decisions about whether to put the object on the stack or on the heap. The fact that the JVM is free to do either shows that it has little effect on the actual behaviour exibited by the Java programs, and is mainly done as a low level optimization technique.
- Oliver
Stefan Ram - 21 Nov 2006 23:58 GMT >Usually people who ask whether an object is "on the stack" or >"on the heap" have a C background, and think that an object >being on the stack or being on the heap is meaningful with >respect to the semantics of the program. It is not clear, what "stack" or "heap" in the context of the programming language C suggests to you, as the C specification ISO/IEC 9899:1999 (E) does not mention "stack" or "heap" anywhere:
http://www.open-std.org/jtc1/sc22/wg14/
Mike Schilling - 22 Nov 2006 07:55 GMT >>Usually people who ask whether an object is "on the stack" or >>"on the heap" have a C background, and think that an object [quoted text clipped - 4 lines] > programming language C suggests to you, as the C specification > ISO/IEC 9899:1999 (E) does not mention "stack" or "heap" anywhere: It is a truth universally acknowledged that C programmers talk in terms of implementation. The most common implementation of C puts automatic variables on a stack and allocates dynamic variables from a heap.
Doug Pardee - 21 Nov 2006 23:44 GMT > That makes sense. So could a single object be on the stack? What about > an object array of constant size? ArrayList[10] or something like that. > Or when you say objects are always stored on the heap, do you really > mean always? Always. In Java, all objects are dynamically allocated with "new", and that means that they're always obtained from the heap. And as defined in section 4.3.1 of JLS3, "An object is a class instance or an array."
So if you have: void someMethod() { int[] array; then the reference variable "array" is on the stack and its initial value is unknown. It is NOT necessarily null; it could be anything. The compiler will try to keep you from accessing "array" until you have set it either to an appropriate array reference or to null.
Now if you have: void someMethod() { int[] array = new int[10]; then the reference variable "array" is still on the stack, but now is initialized to reference an actual array; it has a known value. The array itself is allocated from the heap. Heap allocations are always initialized, so all 10 int values are preset to 0.
Now suppose we have a member variable: public class SomeClass() { int[] array; then the object containing this variable will be allocated on the heap when it is "new"ed so the reference variable "array" is also in the heap. Heap allocations are always initialized, so "array" starts out as null.
How about a static member variable: public class SomeClass() { static int[] array; then the class containing this variable will be allocated on the heap when the class is loaded, so once again the reference variable "array" is also in the heap. Heap allocations are always initialized, so "array" starts out as null.
Patricia Shanahan - 22 Nov 2006 16:35 GMT >> Hi, quick question. Suppose I do: >> [quoted text clipped - 8 lines] > I believe they have to be initialized to 0 according to the JLS. You > would have to check there for a definitive answer. http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#23605
15.10.1 Run-time Evaluation of Array Creation Expressions
"Then, if a single DimExpr appears, a single-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.5.5)."
From section 4.5.5:
* For type byte, the default value is zero, that is, the value of (byte)0. * For type short, the default value is zero, that is, the value of (short)0. * For type int, the default value is zero, that is, 0. * For type long, the default value is zero, that is, 0L. * For type float, the default value is positive zero, that is, 0.0f. * For type double, the default value is positive zero, that is, 0.0d. * For type char, the default value is the null character, that is, '\u0000'. * For type boolean, the default value is false. * For all reference types (§4.3), the default value is null.
Patricia
Mike Schilling - 22 Nov 2006 07:59 GMT > Hi, quick question. Suppose I do: > [quoted text clipped - 8 lines] > Also, and this is unrelated, but does Java have a distinction (to the > programmer) between memory on the stack and memory on the heap? Stack and heap are implementations, not Java concepts. Java does have a distinction between local variables, whose lifetime is scoped to method invocations, and objects, whose lifetime is controlled by the garbage collector. In your example above, x is a local variable and will go away when dumbMethod returns. The array itself does not go away immediately when dumbMethod returns, but since it becomes inaccessible (no references to it remain), it becomes eligible for garbage collection.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|