aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects?" Watch "How many objects?" New topic
Author

How many objects?

Jeff Schuler
Greenhorn

Joined: Apr 17, 2007
Posts: 23
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 ]
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Jeff Schuler:
Are the answers 1

Yes
and 5 respectively?

No
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Manfred Klug
    Ranch Hand

    Joined: Jun 04, 2007
    Posts: 377
    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.
    raj malhotra
    Ranch Hand

    Joined: Feb 22, 2007
    Posts: 285
    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

    Joined: Jun 04, 2007
    Posts: 377
    Hi raj,

    which of the two definitions do you refer to?

    or

    [ June 21, 2007: Message edited by: Manfred Klug ]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Jun 04, 2007
    Posts: 377
    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.
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    And Manfred is right. Cheers.
    Riya Pant
    Greenhorn

    Joined: Feb 17, 2006
    Posts: 28
    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

    Joined: Jun 04, 2007
    Posts: 377
    Originally posted by Riya Pant:
    Am I right?

    Yes.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    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).
    Sergio Tridente
    Ranch Hand

    Joined: Mar 22, 2007
    Posts: 329

    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 ]

    SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
    Manfred Klug
    Ranch Hand

    Joined: Jun 04, 2007
    Posts: 377
    Hi Raghavan,

    the problem was the following statement:
    No,in both the cases there is only one object created.
    Riya Pant
    Greenhorn

    Joined: Feb 17, 2006
    Posts: 28
    Thanks everyone for valuable inputs
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Apr 20, 2006
    Posts: 3344

    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

    Joined: Mar 22, 2007
    Posts: 329

    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

    Joined: Apr 20, 2006
    Posts: 3344

    Thats fine Sergio. It happens sometimes
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: How many objects?