Results 1 to 10 of 10
  1. #1
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Why/how does this work (initializing an array of Objects)

    Hi

    The following compiles:

    Java Code:
    Object o [] = { new Object[1],new Object[2]};
    I thought it would not, because I thought o was an array of Objects, but it seems that

    Java Code:
    new Object[1]
    is somehow the same as

    Java Code:
    new Object()
    In other words, what's the difference between this

    Java Code:
    Object o [] = { new Object(),new Object()};
    and this

    Java Code:
    Object o [] = { new Object[1],new Object[2]};

    I'm confused; can anyone explain please?
    Thanks.
    m

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,776
    Rep Power
    5

    Default Re: Why/how does this work (initializing an array of Objects)

    What you have created is a triangular array of sorts.

    o is an array of object of size 2;
    o[0] = an array of object of size 1;
    o[1] = an array of object of size 2;

    new Object() and new Object[] are different. The first is used to create an object reference. The second is used to create an array of Object reference.

    However, you can't then do this.

    System.out.println(o[0][0]);

    But you can do this.
    Object []bar = (Object[])o[0];
    System.out.println(bar[0]);

    Your original declaration would be better written as

    Object[][] o = {new Object[1], new Object[2]};

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Why/how does this work (initializing an array of Objects)

    Hi Jim
    Sorry, I understand half but when you say


    Your original declaration would be better written as

    Object[][] o = {new Object[1], new Object[2]};


    you seem to imply that given the same RHS assignment (i.e.

    {new Object[1], new Object[2]};

    You could assign it to two different LHS i..e

    Object[][] o = {new Object[1], new Object[2]};

    or

    Object[] o = {new Object[1], new Object[2]};

    Seems strange. Is that what you are saying? It seems that an Object can have some kind of duality wherein it's either an Object or an array of Object. Is that right, and if so is it a peculiarity of Objects alone?

    Thanks,
    m

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default Re: Why/how does this work (initializing an array of Objects)

    An array is-a Object. There's nothing peculiar about that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,776
    Rep Power
    5

    Default Re: Why/how does this work (initializing an array of Objects)

    I said it would be better written only because the syntax of the LHS allows for referencing the assigned array references using two indices instead of having to assign to a secondary array reference.

    Object[][] o1 = {new Object[1], new Object[2]};
    Object[] o2 = {new Object[1], new Object[2]};

    Both of the above accomplish the same thing. It is the syntax of the first which is more flexible.

    Others in this forum may offer a better explanation.

    Regards,
    Jim

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Why/how does this work (initializing an array of Objects)

    Okay Jim, thanks.

    db, you say "An array is-a Object. There's nothing peculiar about that."

    That's kind of getting it backwards. The variable o is declared to be of type "array of Object" and yet it points to "array of "array of Object"".
    So the question becomes, can an Object be an array of Object (rather than, is an array an Object)?

    I just tried it with String instead of Object, and it works for that too


    Object so [] = { new String(),new String()};
    Object so2 [] = { new String[1],new String[2]};


    so2 appears to be pointing to a 2 row array of array of String.
    It seems that somehow an Object is so generic that it can also be an array.

    The following also work, showing presumably that a reference to an Object is so generic that it can point to a multidimensional array of Objects:

    Java Code:
            Object oi = new int[] { 1,2 };
            Object os = new String[5][5];
    ..which is weird because there we are not even declaring an array of Objects.

    It would be of little consequence except this appears in a question in my Java Certification book.
    Thanks,
    m

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why/how does this work (initializing an array of Objects)

    a reference to an Object is so generic that it can point to a multidimensional array of Objects
    Yes, that's it. That multidimensional array is an object (as db says) and hence is a legitimate target for a reference to an Object.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,415
    Rep Power
    20

    Default Re: Why/how does this work (initializing an array of Objects)

    The Javaź Language Specification: Chapter*10.*Arrays

    The first line says it all, but the rest makes good reading too.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Why/how does this work (initializing an array of Objects)

    Quote Originally Posted by pbrockway2 View Post
    Yes, that's it. That multidimensional array is an object (as db says) and hence is a legitimate target for a reference to an Object.
    Ok, got it, thanks very much.

  10. #10
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Why/how does this work (initializing an array of Objects)

    Quote Originally Posted by DarrylBurke View Post
    The Javaź Language Specification: Chapter*10.*Arrays

    The first line says it all, but the rest makes good reading too.

    db
    Interesting, thanks very much.

Similar Threads

  1. Initializing an array of class type variables.
    By xpl0rerchr in forum New To Java
    Replies: 5
    Last Post: 04-10-2012, 10:03 AM
  2. Initializing an array before running a thread
    By AnimeKitty in forum New To Java
    Replies: 1
    Last Post: 01-31-2012, 01:10 PM
  3. Replies: 2
    Last Post: 08-12-2011, 08:48 PM
  4. Replies: 4
    Last Post: 07-19-2011, 01:30 PM
  5. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •