• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many objects?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Schuler:
Are the answers 1


Yes

and 5 respectively?


No
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Ranch Hand
    Posts: 294
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ]
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi raj,

    which of the two definitions do you refer to?

    or

    [ June 21, 2007: Message edited by: Manfred Klug ]
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Ranch Hand
    Posts: 2874
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And Manfred is right. Cheers.
     
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Riya Pant:
    Am I right?


    Yes.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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).
     
    Ranch Hand
    Posts: 329
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ]
     
    Manfred Klug
    Ranch Hand
    Posts: 377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Raghavan,

    the problem was the following statement:

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

     
    Riya Pant
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks everyone for valuable inputs
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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!
     
    Sergio Tridente
    Ranch Hand
    Posts: 329
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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 ]
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thats fine Sergio. It happens sometimes
    reply
      Bookmark Topic Watch Topic
    • New Topic