What does that programming structure mean?

  • Thread starter =?iso-8859-1?B?RnJhbudvaXM=?=
  • Start date
?

=?iso-8859-1?B?RnJhbudvaXM=?=

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.
 
R

Raymond DeCampo

François said:
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
 
J

Jeffrey Schwab

François said:
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)));
}
}
 
?

=?iso-8859-1?B?RnJhbudvaXM=?=

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.
 
K

Kenneth P. Turvey

-----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 <[email protected]>
Phone : (314) 255-2199

XMPP IM: (e-mail address removed)
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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top