• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is an anonymous array?  RSS feed

 
Duc Ta
Ranch Hand
Posts: 41
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read about anonymous array too many times, but I do not know what anonymous array is

The textbook wrote: "We call this anonymnous array creation because with this syntax you don't even need to assign the new array to anything"
Can anyone explain to me, what anonymous is

Best Regards!
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such quotes come from. Is that really the code they used to illustrate that quote, because the two don't seem to match each other?
 
Duc Ta
Ranch Hand
Posts: 41
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please always tell us where such quotes come from. Is that really the code they used to illustrate that quote, because the two don't seem to match each other?


Sorry Mr Campbell Ritchie,
I have read the book
Sun Certified Programmer for Java 6
page 266
I would like to know what anonymous array is, the code above is quoted from this book.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see anything anonymous about that code block. Is that exactly what the book says?
Maybe somebody else has a copy of the book and knows more about what page 266 says. I haven't.

The first line creates a reference with a name. The second line assigns to that reference from an array initialiser. I have never heard that called anonymous anywhere else. You might call it anonymous if you use an initialiser like this:-The array is here used once only and is never given a name, so it might be called anonymous there.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with campbell.

campbell just for your information, jeanne and scott have also used the same term in their famous oca book for array initializer in the context that their type and size are not specified. in my opinion they must have used this term due to the fact that many other books and some online blogs had also used the same naming.

however according to the standard anonymous is used only for anonymous classes due to the fact that the implemented class do not have any name(compiler doesn't know about the name of class during compilation and it is created by jvm during runtime).
so literally if i have to use the term i would just use it in the same context(name).
for array initializers they all are just syntactic sugars. compiler well infers the type and size of even arrays created with array initializers(how, i would not worry about it). so the size and type are also known for arrays like {1, 2, 3} or new int[] {1, 2, 3} during compile time.

look at the method below:

sometimes it is argued that the array initializer like one in main is anonymous in the context that it has not any reference(they just confuse reference with name of the class) but in real their is a reference which is the method parameter 'a' for that array. so in this context also it is not anonymous.
Campbell wrote:You might call it anonymous if you use an initialiser like this:-
The array is here used once only and is never given a name, so it might be called anonymous there.

i would disagree here(please don't get me wrong) as if you will see in the Arrays api the method parameter "a" will be name of the array object so it should not be anonymous.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:i agree with campbell. . . .
I do occasionally get things right.
i would disagree here . . .
Interesting point. You mean that from the view of the code I wrote there is no name for the array, but from inside the toString method it does have a name.
Good point. Maybe we shou‍ld stop saying anonymous for anything other than local/nested classes.
 
Liutauras Vilda
Marshal
Posts: 4656
319
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:

First of all, that code wouldn't compile (assuming you have someObject in place). But anyway...

I'd agree with Campbell, no need to call something anonymous, what JLS not calling. This is a first time in my journey I have heard anonymous array term.
 
Liutauras Vilda
Marshal
Posts: 4656
319
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:jeanne and scott have also used the same term

Can you specificy exact book name and page number, please.

Added: I think you mean OCA book. However, page number would still help.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Interesting point. You mean that from the view of the code I wrote there is no name for the array, but from inside the toString method it does have a name.
Good point. Maybe we shou‍ld stop saying anonymous for anything other than local/nested classes.
exactly!
i wish i could meet you, not for gaining some insights from your knowledge and experience(ranch is already providing that platform) but for feeling the moment when you deliver your sentences. believe me you do it very smart way with mixing some fun there. still i wish i could have that moment.
you are a genius!
for the same reason
I wrote:i would disagree here...
i waffled to wrote those words several times...

Anyway i would like to correct one thing for my previous post where i wrote that the size of the array is known at compile time. i think about it and had done some experiment. look at this code:
it will compile well which infact will throw exception during runtime but compiling well simply connotes that compiler doesn't know about the size of the array so size of an array is anonymous for compiler for every array.
now for the proof that the all the three arrays will be allocated the same way here is the proof. see this dis-assembled code created from javap instruction:
look at line 1, 17, 33 in main they are all instantiated the same way and array type is also int[] for each of them.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras wrote:First of all, that code wouldn't compile (assuming you have someObject in place). But anyway...
yes that wouldn't compile. i wrote it just for clarifying some point, don't take it that way.
Liutaurus wrote:Can you specificy exact book name and page number, please.
Again my fault, i am sorry, i just forget the rule to quote the source or tell the details.
The book name is OCA Oracle certified assosciate java se 8 programmer I study guide by Jeanne Boyarsky and Scott Selikoff. in my edition the page number is 120 chapter 3(Core java api's) under the section creating an array of primitives.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Can you specificy exact book name and page number, please.

The book is
OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808
by Scott Selikoff, Jeanne Boyarsky

The chapter is Chapter 3 Core Java APIs

The section is Creating and Using Arrays

The quote from the book is
As a shortcut, Java lets you write this:
int[] numbers2 = {42, 55, 99};
This approach is called an anonymous array. It is anonymous because you don't specify the type and size.
 
Liutauras Vilda
Marshal
Posts: 4656
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, tracked down about anonymous arrays in OCA Study Guide by Jeanne Boyarsky and Scott Selikoff.

OCA Study Guide by Jeanne Boyarsky and Scott Selikoff, p. 120 wrote:int[] numbers2 = new int[] {42, 55, 99};

In this example, we also create an int array of size 3. This time, we specify the initial values of those three elements instead of using the defaults.
Java recognizes that this expression is redundant. Since you are specifying the type of the array on the left side of the equal sign, Java already knows the type. And since you are specifying the initial values, it already knows the size. As a shortcut, Java lets you write this:
int[] numbers2 = {42, 55, 99};

This approach is called an anonymous array. It is anonymous because you don't specify the type and size.


Alright, there it is. But JLS it calls an array creation using initializers. Don't use any anonymous array terminology.

But apparently this is how authors call it. Probably they had a reason to call it that way. Will know such term too from now on.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another book
A Programmer’s Guide to
Java™ SCJP Certification
Third Edition

Khalid A. Mughal
Rolf W. Rasmussen

On page 74 wrote
The concept of anonymous arrays is similar to that of anonymous classes: they both combine the definition and the creation of objects into one operation.

 
Liutauras Vilda
Marshal
Posts: 4656
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found some kind of blog on web -> http://javarevisited.blogspot.co.uk/2012/01/anonymous-array-example-java-create.html
javarevisited.blogspot.co.uk wrote:Anonymous arrays in Java is an Array without any name, just like Anonymous inner classes and policy of using Anonymous array is just create, initialize and use it, Since it doesn't have any name you can not reuse it.

What they claim, is from different perspective, which sounds to me more convincing, that anonymous array is the one which initialized without assigning to a reference variable, so it can be passed as an argument to some method.

Just simply as: Arrays.sort(new int[]{3, 1, 4, 2})

But if we go that way, then we can call an anonymous Integer too:
printValue(new Integer(4))

I'd probably stick to using terminology which is defined in the Java Language Specification, meaning there is no such thing as an anonymous array - which is even simpler.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both books imply that an anonymous array is.
The JLS says nothing about an anonymous array but says that an array initializer
may be specified as part of an array creation expression, to create an array and provide some initial values.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras wrote:Just simply as: Arrays.sort(new int[]{3, 1, 4, 2})
still here the argument has a name from the inside of the sort method which is 'a' however the reference is unreachable for the user still the fact remains the same which is that array has name.

for me the author uses those terms just for introducing their readers that programmers have just made their own nomenclature and that gots famous. nevertheless they should atleast say something like the standard doesn't call it anything like so.

another similar example is in the same book i reffered in my previous post, the authors use the term modulus operator which is indeed ambigous according to what the modulus means java has already named it remainder operator but they had not even introduced this term. modulus is always non-negative as per euclidean division identity however % is well known to give -ve results.
if jeanne is reading this post their is my request to her for these changes. that would make the concept and the content  more powerful, appealing and simple and straight forward. i hope she will read it.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:Both books imply that an anonymous array is.

No they don't they say is an anonymous array. There is a difference between the two, the second (anonymous) one doesn't specify what type of array to create.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Daniel Cox wrote:Both books imply that an anonymous array is.

No they don't they say is an anonymous array. There is a difference between the two, the second (anonymous) one
doesn't specify what type of array to create.

I think that an anonymous array is new int[] {42, 55, 99}, not {42, 55, 99}

Kathy's book
SCJP Sun® Certified
Programmer for Java™ 6
Study Guide
Exam (310-065)
Kathy Sierra
Bert Bates

page 226 says
You can use two different array-specific syntax shortcuts to both initialize (put explicit values into an array's elements) and construct (instantiate the array object itself) in a single statement. The first is used to declare, create, and initialize in one statement as follows:
1.  int  x = 9;
2.  int[]  dots = {6,x,8};

page 228 says
The second shortcut is called "anonymous array creation" and can be used
to construct and initialize an array, and then assign the array to a previously
declared array reference variable:
int[] testScores;
testScores = new int[] {4,7,2};

You cannot assign {4,7,2} to a previously declared array reference variable:


Also, Khalid's book (on page 74) says
As shown earlier in this section, the following declaration statement

<element type1>[] <array name> = new <element type2>[<array size>]; // (1)

int[] intArray = new int[5];

can be used to construct arrays using an array creation expression. The size of the
array is specified in the array creation expression, which creates the array and initializes
the array elements to their default values. On the other hand, the following
declaration statement

<element type>[] <array name> = { <array initialize list> }; // (2)

int[] intArray = {3, 5, 2, 8, 6};

both creates the array and initializes the array elements to specific values given in the
array initializer block. However, the array initialization block is not an expression.

Java has another array creation expression, called anonymous array, which allows
the concept of the array creation expression from (1) and the array initializer block
from (2) to be combined, to create and initialize an array object:

new <element type>[] { <array initialize list> }

new int[] {3, 5, 2, 8, 6}

The construct has enough information to create a nameless array of a specific type.
Neither the name of the array nor the size of the array is specified. The construct
returns the reference value of the newly-created array, which can be assigned to
references and passed as argument in method calls. In particular, the following two
examples of declaration statements are equivalent.

int[] intArray = {3, 5, 2, 8, 6}; // (1)

int[] intArray = new int[] {3, 5, 2, 8, 6}; // (2)
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Found some kind of blog on web -> http://javarevisited.blogspot.co.uk/2012/01/anonymous-array-example-java-create.html
javarevisited.blogspot.co.uk wrote:Anonymous arrays in Java is an Array without any name, just like Anonymous inner classes and policy of using Anonymous array is just create, initialize and use it, Since it doesn't have any name you can not reuse it.

What they claim, is from different perspective, which sounds to me more convincing, that anonymous array is the one which initialized without assigning to a reference variable, so it can be passed as an argument to some method.

Just simply as: Arrays.sort(new int[]{3, 1, 4, 2})

But if we go that way, then we can call an anonymous Integer too:
printValue(new Integer(4))

I'd probably stick to using terminology which is defined in the Java Language Specification, meaning there is no such thing as an anonymous array - which is even simpler.


According to Khalid's book (on page 75)
The concept of anonymous arrays is similar to that of anonymous classes: they both combine the definition and the creation of objects into one operation.

In this code Arrays.sort(new int[]{3, 1, 4, 2}), the argument you passed (the anonymous array new int[]{3, 1, 4, 2}) combines the definition and the creation of an array object into one operation.

In this code printValue(new Integer(4)), the argument you passed (the Integer object new Integer(4)) does not combine the definition and the creation of an Integer object into one operation. You have to explicitly define the Integer class before you can create an Integer object.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . modulus operator . . .
When I was young, I was taught that modulus is written |x| and is also called absolute value. In non‑English‑speaking Europe the term is used to mean remainder, and there is also the term modulo which has a similar meaning but in terms of a process:
Integer multiplication in done modulo 2³² in Java®
I always say remainder operator myself.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A minute ago wrote:. . . Integer multiplication in done modulo 2³² in Java®
I have probably got that wrong; it was only an example of the usage of modulo.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I confess that I am not the brightest crayon in the box, but I am a tad confused.  In the declaration  int[] somearray = {3, 1, 2, 4};    doesn't the int[] declare the array as type integer?  I don;t understand how/why this declaration implies the array is untyped.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:I don;t understand how/why this declaration implies the array is untyped.

The array is not untyped. What documentation says that the array is untyped?

Are you referring to Jeanne's book?

Jeanne's book says that

is a shortcut for

You don't have to specify the type again since Java already knows the type.


 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:. . . doesn't the int[] declare the array as type integer? . . .
Careful about words: there is integer, there is Integer, and there is int. The three are subtly different.
The declaration int[] declares the reference as type int‑array, which is usually abbreviated to [I by getClass().getName(). Since Java® is a strongly typed language it is only possible to put something of type  int‑array or [I into it. Since the typing is done statically, any typing errors shou‍ld become obvious at compile time. Daniel Cox is right: the compiler can infer the type from the elements in the code ...{1, 2, 3}...
Actually, inferring such types is by no means difficult for a compiler to do.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Cox wrote:
I think that an anonymous array is new int[] {42, 55, 99}, not {42, 55, 99}


But that contradicts the text you quoted earlier. ie

Daniel Cox wrote:The chapter is Chapter 3 Core Java APIs

The section is Creating and Using Arrays

The quote from the book is
As a shortcut, Java lets you write this:
int[] numbers2 = {42, 55, 99};
This approach is called an anonymous array. It is anonymous because you don't specify the type and size.

And my comments were in regard to this quote.

But the following text supports your view.
Daniel Cox wrote:

Also, Khalid's book (on page 74) says
...
Java has another array creation expression, called anonymous array, which allows
the concept of the array creation expression from (1) and the array initializer block
from (2) to be combined, to create and initialize an array object:

new <element type>[] { <array initialize list> }

new int[] {3, 5, 2, 8, 6}

The construct has enough information to create a nameless array of a specific type.
...

It would appear there is some confusion on this.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Daniel Cox wrote:
I think that an anonymous array is new int[] {42, 55, 99}, not {42, 55, 99}


But that contradicts the text you quoted earlier. ie

Daniel Cox wrote:The chapter is Chapter 3 Core Java APIs

The section is Creating and Using Arrays

The quote from the book is
As a shortcut, Java lets you write this:
int[] numbers2 = {42, 55, 99};
This approach is called an anonymous array. It is anonymous because you don't specify the type and size.



You're right. That looks like an errata in Jeanne's book. An array initializer ({42, 55, 99}) is not an anonymous array (new int[] {42, 55, 99}).
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell wrote:...Integer multiplication in done modulo 2³² in Java®
...I have probably got that wrong...
might be!
since even the jvm specs had not involve any such details regarding it. however it just mentions some instruction sets or mnemonics for its assembly like imul which might get delegate to different instruction set for your and mine computers processor. there is a chance that it will use modulo 2³² on yours and some other algorithm on mine. however since implementation matters here i would like to keep my hands away from it in view of the standards main aim.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having seen this discussion go round and round, I think I shall agree with PK and reserve anonymous for before the wor‍d class.
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You reserve the right to disagree with the authors mentioned in this thread.
 
Peter Lawlor
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Guys
Thank you for the 'opportunity' to be a part/section of the 'Java Ranch' and all the 'Java (family) Forums'.
Am not sure which 'part' (or whatever section) of the 'Java People' am writing to now, (i know it's an Electronic Letter), but to whover you are,
I would just like to say a 'Big Thank You'. Also a Huge Hug to All of you for the great feelings.
From my Heart,
Peter ...
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch, Peter Lawlor!

The ranch has a new forum that can be used for introductions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!