Java Reference
In-Depth Information
The Array object can take zero or more number of arguments. Its initialization
behavior depends on the number and types of passed arguments that can be classified in
three categories:
No arguments are passed
One argument is passed
Two or more arguments are passed
Passing No Arguments
When no arguments are passed to the Array constructor, it creates an empty array, setting
the length of the array to zero:
var names1 = new Array(); // Same as: var names = [];
Passing One Argument
When one argument is passed to the Array constructor, the type of the argument
determines how the new array is created:
If the argument is a Number and it is an integer in the range from
0 to 2 32 -1 (inclusive), the argument is considered the length of the
array. Otherwise, a RangeError exception is throw.
If the argument is not a Number, an array with the passed
argument as the sole element of the array is created. The length
of the array is set to 1.
The following code creates the biggest possible array in Nashorn:
var names = new Array(Math.pow(2, 32) -1); // The biggest possible array
print("names.length = " + names.length);
names.length = 4294967295
The following statement creates an array with length as 10. No elements exist in the
array yet:
var names = new Array(10);
The following array creation expressions throws a RangeError exception, because
the argument is a Number and it is either not an integer in the valid range or out of range:
var names1 = new Array(34.89); // Not an integer
var names1 = new Array(Math.pow(2, 32)); // Out of range
var namess = new Array(-10); // Out of range
 
Search WWH ::




Custom Search