Arrays In Java
Arrays in java are the collection of data of same data types.
Arrays have more than one values but all of these are are of one data type.
In general, we can say about the arrays that, arrays are the collection of the variables of the same data type.
- Arrays are very useful when you have large number of variables and you have to initialize them example, from 0 to 100, Then arrays become very common in use you can use array to initialize your all variables.
- Initializing all the variables independent variables is memory loss, using arrays saving your memory in greater extent.
- Arrays index starting from 0, it means that if there are n numbers are present in the array then the index of the last element of array is n-1;
- The size of the array must be initialized by int value neither long nor short and byte.
Memory location for elements in the array:

In the above example You can clearly see that arrays index is starts from 0.
- In the above example the the length of array is 6 but index of the last element is 5 as shown above.
- As I mentioned above that if arrays length in ’n’ then there will ‘n-1’ is the index number of the array elements.
- You can see that the first element is starts from zero.
- Length of the array is simply the number of elements present in the array.
- The last index of the array is ‘array length-1' as you see in above figure;
declaration of an Array:
The arrays can be declared as follows:
data-type name-Of-Array[];
OR
data-type[] name-Of-Array;
- During the declaration of an Array we have two components.
- data-type:
The data-type is the data-type of the elements of the array.The data-types shows that an array is consists of what type of the elements.
Data-type shows the data-types of the elements of the array like, int, char, float, double, String etc. or user defined data-types(objects of the classes).
2. Name-Of-array;
It simply shows the name of the array.
The name of the arrays must not be the primitive data-types like, int, String, char and float. These are the primitive data types in the java You must avoid to name your arrays like built in data types.
It would be better that you should name the array like, arr, arr1, arr2,a, b etc. You can name your array you but must not be the built in data types.
For Example:

Initialize An Array:
- Declaration of array just creates a reference of an array.
- For allocating the memory you must have to initialize the array, Means give elements to the array of the specific data-type.
- The array should be initialize as below:
data-type = new type [size];The example above shows the initialization of the array.
- In above example we have to say which type of elements of the array, and int large brackets we have say how many elements should this array can hold.
- The new keyword is user for to create object of the array and by using this ‘new keyword’ we are giving memory to our array.
- The size of an array must be in int data type.
Example
int newArray[]; // it declares an array
newArray = new int[20]; // it initialize an array of size 20 of data-type int;
Or
char newArray = new char[30]; //it initialize an array of size 30 of data-type 30;
new keyword:
- new keyword automatically fill the required length of an with some default values depending upon the data type of the array.
Example:
int:
If the data-type of an array is int then new keyword fills all of its size by its default number which is zero.
Means all the elements in an array are zero.
String:
If the data type of an array is String then new keyword fill the size of an array by its default value ‘null’;
Same process for double and boolean.
For double array new keyword stores 0.0 as default in an array, and false for boolean array.
During initialization we also pass the direct values to an array as follows.
int[] newArray = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
In the above example we pass all the values to an array.
There is no need of passing the argument of size, the size of an array is seen by the counting of the all elements of an array.
The first index of an array is starts from 0 and ends at (array length)-1.
Accessing the elements of an array Using For loop
for(int i = 0; i < newArray.length; i++){
System.out.println(“Element in ”+i+” index is = ”+newArray[i]);
}
Throw the above example we can traverse all the elements of an array.
The For loop starts from 0 and ends at arr.length-1, that’s why we use i < newArray.length, which shows that one less than the actual length of an array. If we write i ≤ newArray.length the JVM throws ArrayOutfBoundException, which shows that the index of an array is negative or greater than or equal to length of an array, which is known as illegal index of an array.
- Note
If you want to initialize the elements of an array individual then you have to do:
int[] arr = new arr[3]; // allocates the memory to an array before initialize its elements;
Now You have to do:
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
OR
Simply you have to run a for loop to initialize all indexes of an array as follows:
for(int i = 0; i < arr.length; i++){
arr[i] = i;
}
In the above example you are initializing the all the indexes of an array to the values of i.
Code for initializing and traversing the elements of an array is:
class Array{
public static void main(String args[]){
int [] arr = new int[5];
for(int i = 0; i < arr.length; i++){
arr[i ] = i;
}
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i])
}
}
}
