Java Array Creation
Since arrays are java objects, it can be created using new operator
using following syntax.
arrayName = new <type>[<size>];
Here size must be byte, char, short or int. Any attempt to create
an array with long will result in compilation error. If the array
size is negative, NegativeArraySizeException is thrown.
When create arrays are initialized automatically to their default
values.
Note: In java, it is perfectly legal to create array with size
0.
Java array creation examples are given below.
myIntArray = new int[10];
myStringArray = new String[10];
myObjectArray = new MyObject[10];
Array declaration and creation can be combined into one statement
as follows.
int myIntArray = new int[10];
String myStringArray = new String[10];
MyObject myObjectArray = new MyObject[10];
|