File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Creating anonymous arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Creating anonymous arrays" Watch "Creating anonymous arrays" New topic
Author

Creating anonymous arrays

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Is there any difference between hwat actually occurs when you perform the following two lines of code?

Basically, I'm showing two ways to define an anonymous array. Is there any difference between what happens when the first anonymous array is created (without using new) and what happens when the second anonymous array is created (using new)?
Thanks,
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey,
Your first example is an array created using an array initializer and the second example is an array created using an array creation expression WITH and array initializer.
There are not much differences between the two.
For further information, JLS 10 Arrays
[ February 25, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Valentin Crettaz:
There is no such thing as "anonymous arrays" in Java.

Val,
I just want to make sure that I have the terminology correct. I've been studying out of Mughal and Rasmussen and they have a section on Anonymous Arrays (that's page 93, for those keeping score at home). Look at the following code:

This code compiles and runs fine, printing out the contents of the array: 1, 2, 3.
Now, what do you call the array that is created at line 1? Wouldn't that be considered an "anonymous array?" Or do you simply refer to this as an array initializer and nothing more?
Thanks again,
Corey
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sorry, yes it is an anonymous array but those you provided are not. See the difference:
In the book:
doIt(new int[] {1,2,3}); // the array has no name
your example:
int[] a = {1,2,3,4,5};// first array is called a
int[] b = new int[] {1,2,3,4,5}; // second array is called b
So they are not anonymous...
An anonymous array is an array usually created within a method invocation expression (just as in the example).
Anonymous means "not known by name" which is not the case in your example...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating anonymous arrays