In this section, you will learn how to declare array in Java.
Java Array Declaration
As we declare a variable in Java, An Array variable is declared the same way. Array variable has a type and a valid Java identifier i.e. the array's type and the array's name. By type we mean the type of elements contained in an array. To represent the variable as an Array, we use [] notation. These two brackets are used to hold the array of a variable.
By array's name, we mean that we can give any name to the array, however it should follow the predefined conventions. Below are the examples which show how to declare an array :-
int[] array_name;
//declares an array of integers String[] names; int[][] matrix; //this is an array of arrays |
It is essential to assign memory to an array when we declare it. Memory is assigned to set the size of the declared array. for example:
int[] array_name
= new int[5]; |
Here is an example that creates an array that has 5 elements.
public class Array |