We declare an array in Java as we do other variables, by providing a type and name: int[] myArray; To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15};
How do you instantiate an array?
To instantiate an array, use this syntax: arrayName = new datatype[ size ]; where size is an expression that evaluates to an integer and specifies the number of elements. When an array is instantiated, the elements are assigned default values according to the array data type.
How do we instantiate an array in Java?
Initializing an array
- class HelloWorld { public static void main( String args[] ) { //Initializing array. int[] array = new int[5]; ...
- class HelloWorld { public static void main( String args[] ) { //Array Declaration. int[] array; ...
- class HelloWorld { public static void main( String args[] ) { int[] array = {11,12,13,14,15};