In general, Array is a collection of data objects of the same data type,
It is incredibly useful for solving programming problems,
To declare an array in java,
The syntax is:
datatype[ ] array_name;
(datatype) The type of Objects that will be stored in the array eg. Int, float, char, double, etc…
“ [ ] ” Specifies that the declared variable points to an array.
(array_name) Specifies the variable name of the array.
Example: int[ ] sum;
To initialize an array in java,
The syntax is:
datatype[ ] array_name = new datatype[size];
An array can be initialized to a particular size and the default value of each element is 0.
Example: int[ ] result =new int[10];
Here the array of variable is initialized without assigning the values with the size of 10 and you actually reserve 10 int space from memory, (0-9) for array element and +1 for array itself.
With assigning values:
datatype[ ] array_name = new datatype[size]{no. of values separated by comma(,)};
Example:
int[ ] add=new int[ ]{1,2,3,4,5};
When assigning values to an array during initialization, the size is not specified.
For simple, initialization and declaration can also be done in a single statement,
In general, Array is a collection of data objects of the same data type,
It is incredibly useful for solving programming problems,
To declare an array in java,
The syntax is:
datatype[ ] array_name;
(datatype) The type of Objects that will be stored in the array eg. Int, float, char, double, etc…
“ [ ] ” Specifies that the declared variable points to an array.
(array_name) Specifies the variable name of the array.
Example: int[ ] sum;
To initialize an array in java,
The syntax is:
datatype[ ] array_name = new datatype[size];
An array can be initialized to a particular size and the default value of each element is 0.
Example: int[ ] result =new int[10];
Here the array of variable is initialized without assigning the values with the size of 10 and you actually reserve 10 int space from memory, (0-9) for array element and +1 for array itself.
With assigning values:
datatype[ ] array_name = new datatype[size]{no. of values separated by comma(,)};
Example:
int[ ] add=new int[ ]{1,2,3,4,5};
When assigning values to an array during initialization, the size is not specified.
For simple, initialization and declaration can also be done in a single statement,
Ex: int[ ] add={1,2,3,4,5};