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