Home Java SCJP SCWCD Servlet Submit News Contact Us Site Map


Over 500 magazines for free (including Oracle Magazine)!

Yes all of them are FREE. You can subscribe to ALL of them.
Click here to apply today!

Home > Java > Java Articles

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];

 

<< Array

Array Initialization >>


Home Java SCJP SCWCD Servlet Site map