Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
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;
myStringArray = new String;
myObjectArray = new MyObject;
Array declaration and creation can be combined into one statement as follows.
int myIntArray = new int;
String myStringArray = new String;
MyObject myObjectArray = new MyObject;
<< Array Array Initialization >>