Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating anonymous arrays

 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic