What does that programming structure mean?

Discussion in 'Java' started by =?iso-8859-1?B?RnJhbudvaXM=?=, Feb 25, 2006.

  1. Hi,

    I've come across that programming structure, and been wondering the
    exact meaning of it:

    new Object[] {new Integer(maxPercentage)}

    As I understand it, it instantiates an array of Objects with a single
    integer.

    But then, we have the { }: I would have expected ( ).

    It has the structure of a class declaration. But then, no ";" at the
    end of "new Integer(maxPercentage)"

    Quite boggled as to know what does that programming structure exactly
    perform, and what is assigned to what. In other words, what is
    accomplished here?

    >From what I understand, I would have written that code line:


    new Object(new Integer(maxPercentage))

    Building a new Object with the constructor of the class, with a new
    Integer. Of course, this doesn't compile.

    Thanks for your help.

    F.
     
    =?iso-8859-1?B?RnJhbudvaXM=?=, Feb 25, 2006
    #1
    1. Advertising

  2. François wrote:
    > Hi,
    >
    > I've come across that programming structure, and been wondering the
    > exact meaning of it:
    >
    > new Object[] {new Integer(maxPercentage)}
    >
    > As I understand it, it instantiates an array of Objects with a single
    > integer.
    >
    > But then, we have the { }: I would have expected ( ).
    >
    > It has the structure of a class declaration. But then, no ";" at the
    > end of "new Integer(maxPercentage)"
    >
    > Quite boggled as to know what does that programming structure exactly
    > perform, and what is assigned to what. In other words, what is
    > accomplished here?
    >
    >>From what I understand, I would have written that code line:

    >
    > new Object(new Integer(maxPercentage))
    >
    > Building a new Object with the constructor of the class, with a new
    > Integer. Of course, this doesn't compile.
    >


    This is simply the syntax for declaring the contents of an array
    in-line. It isn't built upon other syntaxes, do not try to understand
    it that way.

    The code

    Object[] arr = new Object[] {new Integer(maxPercentage)};

    is equivalent to

    Object[] arr = new Object[1];
    arr[0] = new Integer(maxPercentage);

    You can use this idiom in places where the full blown version would be
    awkward, e.g.

    return new Object[] {new Integer(maxPercentage)};

    Sometimes you can neglect the new portion and use just the braces:

    Object[] arr = {new Integer(maxPercentage)};

    Obviously, you can create arrays with multiple elements:

    String[] arr1 = {"one", "two", "three"};
    String[] arr2 = {"1", "2", "3", };
    // Note that the last comma is not a syntax error

    You can also do multidimensional arrays:

    String[][] arr = {
    {"one", "two", "three"},
    {"1", "2", "3", },
    };

    HTH,
    Ray

    --
    This signature intentionally left blank.
     
    Raymond DeCampo, Feb 25, 2006
    #2
    1. Advertising

  3. François wrote:

    > I've come across that programming structure, and been wondering the
    > exact meaning of it:
    >
    > new Object[] {new Integer(maxPercentage)}
    >
    > As I understand it, it instantiates an array of Objects with a single
    > integer.


    That's right. Ray has already explained this better than I could, but
    if you want to check things like this in the future, you might try
    something like the code at the bottom of this post.

    > From what I understand, I would have written that code line:
    >
    > new Object(new Integer(maxPercentage))
    >
    > Building a new Object with the constructor of the class, with a new
    > Integer. Of course, this doesn't compile.


    Because Object has no public constructor taking a reference to an extant
    object.

    ------------------------------------------------------------------------

    import java.io.PrintWriter;
    class Main {
    static PrintWriter out = new PrintWriter(System.out, true);

    static <T> void printType(T[] a) {
    Class c = a.getClass();
    out.println(c.getSimpleName() + ":");

    for(T t : a) {
    out.println("\t" + t);
    }
    }

    static <T> void printType(T t) {
    Class c = t.getClass();
    out.println(c.getSimpleName() + ":");
    out.println("\t" + t);
    }

    public static void main(String[] args) {
    int maxPercentage = 100;
    printType(new Object[] {new Integer(maxPercentage)});
    printType(new Integer(maxPercentage));

    /* Not legal: No such constructor in Object. */
    //printType(new Object(new Integer(maxPercentage)));
    }
    }
     
    Jeffrey Schwab, Feb 25, 2006
    #3
  4. Thank you Ray,
    Much appreciation for the clear explanation!

    Thanks Jeffrey,
    Interesting piece of code. I'll try it latter on. It's using the new
    templating functionality of Java (<T>), no? The old C++ STL's got a
    new friend...

    F.
     
    =?iso-8859-1?B?RnJhbudvaXM=?=, Feb 25, 2006
    #4
  5. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Sat, 25 Feb 2006 11:20:11 -0800, François wrote:

    [Snip]
    > I've come across that programming structure, and been wondering the
    > exact meaning of it:
    >
    > new Object[] {new Integer(maxPercentage)}
    >
    > As I understand it, it instantiates an array of Objects with a single
    > integer.


    That's correct. You could have put a comma after it and put more integers
    in the array:

    new Object[] {new Integer(oneValue), new Integer(twoValue)};


    > Quite boggled as to know what does that programming structure exactly
    > perform, and what is assigned to what. In other words, what is
    > accomplished here?
    >


    [Snip]
    >From what I understand, I would have written that code line:
    >
    > new Object(new Integer(maxPercentage))

    [Snip]

    This wouldn't give you the same thing. This gives you a single object the
    previous gives you an array of Objects that just happens to only have one
    entry in the array.


    - --
    Kenneth P. Turvey <>
    Phone : (314) 255-2199

    XMPP IM:
    Yahoo IM: kpturvey2
    AIM IM: kpturvey3
    ICQ IM: 320005929
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFEAQGgi2ZgbrTULjoRAvx/AJ4k2oo53njeF4BUwnbmvN2wcEWoLwCbBkVJ
    l4pux8XsyFwI/SOyYegRYqM=
    =pjUn
    -----END PGP SIGNATURE-----
     
    Kenneth P. Turvey, Feb 26, 2006
    #5
    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. raghu

    what the statement in a structure mean

    raghu, Nov 13, 2006, in forum: C Programming
    Replies:
    18
    Views:
    480
    CBFalconer
    Nov 14, 2006
  2. Frederiek
    Replies:
    3
    Views:
    416
    Jakob Bieling
    Jul 11, 2006
  3. Li Ma
    Replies:
    1
    Views:
    2,363
    Roedy Green
    Mar 9, 2009
  4. Rahul
    Replies:
    4
    Views:
    624
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,283
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page