Forums Register Login

How many objects?

+Pie Number of slices to send: Send
How many objects are on the heap after this line executes?

---------
int[][] dots3 = new int[3][];
---------


How many objects are on the heap after this line executes (assume different program)?

---------
int[][] dots3 = new int[3][4];
---------


Are the answers 1 and 5 respectively?
[ June 20, 2007: Message edited by: Jeff Schuler ]
+Pie Number of slices to send: Send
 

Originally posted by Jeff Schuler:
Are the answers 1


Yes

and 5 respectively?


No
+Pie Number of slices to send: Send
No,in both the cases there is only one object created.

You may get confused by just seeing the dimensions get varying, but infact they are just the placeholders for the size (room allocation).

Until and unless you manually create an object and assign to the index of the array, there are NO extra objects created.

Say, for example



Now, there are 3 objects

  • one object represented by the 2d array
  • two objects represented each by the 1d arrays



  • HtH.
    +Pie Number of slices to send: Send
     

    Originally posted by Raghavan Muthu:
    int[][] int2DArray = new int[2][2]; //here only one object!


    Not correct. Remember, on the heap are all arrays one dimensional.

    Originally posted by Raghavan Muthu:

    /* Assigning the arrays to the 2D array object */
    int2DArray[0] = int1DArray1;
    int2DArray[1] = int1DArray2;
    [/CODE]


    And those two statements give you the hint how multidimensional arrays are organized.

    If you have doubts, have a look at 'Creating, Initializing, and Accessing an Array' in the Java tutorial.
    +Pie Number of slices to send: Send
    I think only one object will be created only the dots3 reference variable will be provided enough space to hold reference of the 3 individual arrays of size 4 which are not created yet.
    [ June 21, 2007: Message edited by: raj malhotra ]
    +Pie Number of slices to send: Send
    Hi raj,

    which of the two definitions do you refer to?

    or

    [ June 21, 2007: Message edited by: Manfred Klug ]
    +Pie Number of slices to send: Send
    Hello Manfred,


    Originally posted by Manfred Klug:
    Not correct. Remember, on the heap are all arrays one dimensional.



    I haven't mentioned anything about the dimensions while saying about the object counts. I just said that, "there is only one object created"!..

    I do agree with you that the array itself is just an object and nowhere it holds the dimensions for object!! But i have not mentioned that a 2D object is created.


    Originally posted by Manfred Klug:
    And those two statements give you the hint how multidimensional arrays are organized.



    What exactly you are trying to say? Can you please elaborate a bit more on this?
    +Pie Number of slices to send: Send
    Hi Raj,


    Originally posted by Raj malhotra:
    I think only one object will be created only the dots3 reference variable will be provided enough space to hold reference of the 3 individual arrays of size 4 which are not created yet.



    Yes, thats perfectly correct.


    Originally posted by Manfred Klug:
    which of the two definitions do you refer to?



    Manfred, Raj's statements seems to be referring to the second one which holds 3*4 dimension.
    +Pie Number of slices to send: Send
     

    Originally posted by Raghavan Muthu:
    What exactly you are trying to say? Can you please elaborate a bit more on this?



    Quote from 'Creating, Initializing, and Accessing an Array' in the Java tutorial

    In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran.


    As consequence, the statement creates four objects.

    One array of array and three arrays of int.
    +Pie Number of slices to send: Send
    And Manfred is right. Cheers.
    +Pie Number of slices to send: Send
    Hi,
    I am still not very clear.Let me put it again what I understand:
    If we write
    int[][] array = new int[3][]; Then 1 object is created.
    And if I write
    int[][] array = new int[3][4]; Then 4 objects are created.
    Am I right?
    +Pie Number of slices to send: Send
     

    Originally posted by Riya Pant:
    Am I right?


    Yes.
    +Pie Number of slices to send: Send
    That's fine Manfred. Thanks.


    int[][] int2DArray = new int[2][2]; //here only one object!



    But i was intended to say only one object for the 2D array object "int2DArray". I should have made a note in the comment!

    During the second statement of OP, it creates 4 objects. You are right. This holds good when you fill both the dimensions for the 2D Array (or all n dimensions for a nD Array).
    +Pie Number of slices to send: Send
    Hi Raghavan,

    Take a look at this modified version of your previous example:



    This runs fine too, which confirms what Manfred said before.

    [Edit] Sorry, I did not see your previous answer.
    [ June 21, 2007: Message edited by: Sergio Tridente ]
    +Pie Number of slices to send: Send
    Hi Raghavan,

    the problem was the following statement:

    No,in both the cases there is only one object created.

     
    +Pie Number of slices to send: Send
    Thanks everyone for valuable inputs
    +Pie Number of slices to send: Send
    Hi Sergio,

    Thats fine. I had a small confusion and that was a mistake. I had told in one of the previous replies.


    During the second statement of OP, it creates 4 objects. You are right. This holds good when you fill both the dimensions for the 2D Array (or all n dimensions for a nD Array).



    I dont get your statement below..


    Originally posted by Sergio:
    [Edit] Sorry, I did not see your previous answer.



    Can you clarify?
    +Pie Number of slices to send: Send
    Hi Manfred,


    Hi Raghavan,

    the problem was the following statement:

    quote:No,in both the cases there is only one object created.



    Yes, that was a mistake I had corrected the same and mentioned in my previous reply.

    Thanks again for pointing out!
    +Pie Number of slices to send: Send
     

    Originally posted by Raghavan Muthu:
    Can you clarify?



    Don't worry. I was just refering to your post just above mine. (When I began to answer it wasn't there, so I apologized for repeating what you had just said.)
    [ June 21, 2007: Message edited by: Sergio Tridente ]
    +Pie Number of slices to send: Send
    Thats fine Sergio. It happens sometimes
    I'm still in control here. LOOK at this tiny ad!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 746 times.
    Similar Threads
    Garbage Collection Q & A
    One more time :) G. C. Q and A
    Objects created on Heap
    need help in gc!!!!!
    Garbage Collector
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 10:35:14.