This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous array" Watch "Anonymous array" New topic
Author

Anonymous array

Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
What is an anonymous array and in what context is it used?
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi!
Anonymous array:
String str=new String[]{"fudong","jia"}[0];//size must not be specified
System.out.println(str);

the output is:fudong


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Anonymous arrays are used when they are passed as an argument to a method that expects an array.


Hope this clears your doubts.
Thanks
Deepak


SCJP, SCWCD, SCBCD
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi,

Anonymous arrays - as the name indicates the sole purpose of it has to go anonymous!

When do you want an anonymous array? An anonymous means a person with NO specific name! Similarly, an anonymous array is an array with no specific reference to it.



Here, the newly created array of Integers is referred through a reference variable 'intWrapperArray'. This is the usual practice we follow because, at a later point of time, we need the same array to be referred. It is possible only thorugh a hold/pointer/reference.

Is there any situation wherein we DO NOT need any such reference? Fine, If at all we have a situation, that's when we go for the anonymous array!

The situation is like when you want to pass an array of values (primitives or objects) but you do not really want to keep a reference to the passed values. That's when you just create an array and pass it. They can also be called as 'Just in time Arrays'. But not so sure whether such term exists or not.

I guess Deepak's example will help you.

Cheers,
Raghavan alias Saravanan M.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Good insight there Muthu,
Once an anonymous array is passed as an reference to a method it will no longer be visible to the caller unless offcourse its reference is saved in the called method.
Thanks
Deepak
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thank you Deepak!

Muthu is my surname. I can be called as 'Raghavan' - just fyi
 
Don't get me started about those stupid light bulbs.
 
subject: Anonymous array
 
Similar Threads
Anonymous array
Anonymous Array
Anonymous Array
anonymous arrays
Anonymous Arrays