array intialization for primitives

E

Eric

Hi, quick question. Suppose I do:

void dumbMethod (int i) {
int[] x;
x = new int;
}

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?
 
R

Robert Klemme

Eric said:
Hi, quick question. Suppose I do:

void dumbMethod (int i) {
int[] x;
x = new int;
}

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
 
E

Eric

Robert said:
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
 
R

Robert Klemme

Eric said:
Robert said:
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.

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?

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
 
O

Oliver Wong

Eric said:
Robert said:
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?

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
 
D

Doug Pardee

Eric said:
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.
 
S

Stefan Ram

Oliver Wong said:
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/
 
M

Mike Schilling

Stefan Ram said:
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:

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.
 
M

Mike Schilling

Eric said:
Hi, quick question. Suppose I do:

void dumbMethod (int i) {
int[] x;
x = new int;
}

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?


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.
 
P

Patricia Shanahan

Robert said:
Eric said:
Hi, quick question. Suppose I do:

void dumbMethod (int i) {
int[] x;
x = new int;
}

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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top