array intialization for primitives

Discussion in 'Java' started by Eric, Nov 21, 2006.

  1. Eric

    Eric Guest

    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?
     
    Eric, Nov 21, 2006
    #1
    1. Advertising

  2. Eric <> wrote:
    > 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
     
    Robert Klemme, Nov 21, 2006
    #2
    1. Advertising

  3. Eric

    Eric Guest

    Robert Klemme wrote:
    > 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
     
    Eric, Nov 21, 2006
    #3
  4. Eric <> wrote:
    > Robert Klemme wrote:
    >> 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
     
    Robert Klemme, Nov 21, 2006
    #4
  5. Eric

    Oliver Wong Guest

    "Eric" <> wrote in message
    news:ejvtl6$aa7$...
    > Robert Klemme wrote:
    >
    >> 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
     
    Oliver Wong, Nov 21, 2006
    #5
  6. Eric

    Doug Pardee Guest

    Eric wrote:
    > 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.
     
    Doug Pardee, Nov 21, 2006
    #6
  7. Eric

    Stefan Ram Guest

    "Oliver Wong" <> writes:
    >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/
     
    Stefan Ram, Nov 21, 2006
    #7
  8. "Stefan Ram" <-berlin.de> wrote in message
    news:-berlin.de...
    > "Oliver Wong" <> writes:
    >>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:


    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.
     
    Mike Schilling, Nov 22, 2006
    #8
  9. "Eric" <> wrote in message
    news:ejvsjh$9rf$...
    > 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.
     
    Mike Schilling, Nov 22, 2006
    #9
  10. Robert Klemme wrote:
    > Eric <> wrote:
    >> 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
     
    Patricia Shanahan, Nov 22, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john
    Replies:
    2
    Views:
    435
    valentin tihomirov
    Jul 5, 2005
  2. john

    Intialization

    john, Sep 22, 2005, in forum: VHDL
    Replies:
    0
    Views:
    519
  3. Replies:
    1
    Views:
    962
    Stefan Schulz
    Jan 2, 2006
  4. =?iso-8859-1?Q?Juli=E1n?= Albo

    Members of anonymous unions in intialization lists

    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 25, 2003, in forum: C++
    Replies:
    1
    Views:
    330
    Victor Bazarov
    Nov 25, 2003
  5. Drak I

    Array intialization

    Drak I, Jun 23, 2004, in forum: C++
    Replies:
    5
    Views:
    2,178
    Mats Weber
    Jun 23, 2004
Loading...

Share This Page