Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

What does that programming structure mean?

Thread view: 
François - 25 Feb 2006 19:20 GMT
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.
Raymond DeCampo - 25 Feb 2006 20:17 GMT
> Hi,
>
[quoted text clipped - 21 lines]
> 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

Signature

This signature intentionally left blank.

Jeffrey Schwab - 25 Feb 2006 20:35 GMT
> I've come across that programming structure, and been wondering the
> exact meaning of it:
[quoted text clipped - 3 lines]
> 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)));
    }
}
François - 25 Feb 2006 21:55 GMT
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.
Kenneth P. Turvey - 26 Feb 2006 01:17 GMT
[Snip]
> I've come across that programming structure, and been wondering the
> exact meaning of it:
[quoted text clipped - 3 lines]
> 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 <kt-usenet@squeakydolphin.com>
Phone : (314) 255-2199

XMPP  IM: kpturvey@jabber.org
Yahoo IM: kpturvey2
AIM   IM: kpturvey3
ICQ   IM: 320005929


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.