File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whizlab mock : array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Whizlab mock : array" Watch "Whizlab mock : array" New topic

Whizlab mock : array

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186

there is an compile error at line 2,
whizlabs stated that -
"when we use this syntax to create arrays, we should not specify the size, the size is evaluated from the number of elements between the curly braces."

but isn't line 1 doing as such?
from any introduction of array tutorial, the most basic declaration would be
int[] table = new int[4]
not in curly braces?

does the annoynomous class has anything to do with it?
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
That's not an anonymous class, but an anonymous array. The general syntax is:

new type[] { value1, value2, ..., valueN }

As you can probably tell, this syntax is just an extension of the normal array initializer syntax. Its advantage is that it allows you to express an array value without having to declare a local variable. It's particularly convenient for method parameters and return values, e.g.

As you found out, however, with this syntax you must not include the length of the array within the square brackets. The array length will be determined automatically from the number of elements within the curly braces.

SCJP 5.0
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
I agree. Here's the link:
subject: Whizlab mock : array
It's not a secret anymore!