Java Array | Declaration, Initialization, Example Code

Java Array | Declaration, Initialization, Example Code

Array in Java is an important concept and, in this article, we are going to understand the array and its working.

Java array is an object that is used to hold multiple elements of the same data type in a single variable. The length of the array is fixed and it is established when the array is created. The elements of the array have contiguous memory allocation and can be accessed by numerical index.

Array index begins from 0 i.e., the first element will have the 0th index, the second element will have the 1st index, and so on. The length of the array is fixed and it is established when the array is created.

How to Declaring Array?

Declaring an array is just like declaring a variable. We need to write the data type of array and then the name of the array. Type of array is written as type[] where type is any data type of array like int, float, etc.

Here, the square bracket indicates that the variable is an array. The name of the array can be anything but must follow the naming convention. Declaration only specifies the name and type of variable so declaring an array does not mean creating an array, it simply indicates that the variable will contain an array of specified datatype.

See the below declaration:

int[] arr; 

This will create a variable named arr as an integer array.

You can also place the brackets after the name of the array. But according to the Java coding convention, it is not advised to use this form as brackets are used to identify array type so it should appear with the type designation.

Creating, Initializing, and Accessing an Array

To create an array, we need to use the new operator as we did here:

arr= new int[5]

If the above statement is compiled, an integer array arr of size 5 will be created and memory space will be allocated to it.

We can initialize an array by assigning values to the elements of the array. We can do so by accessing the array using the index value.

arr[0]=1;  // first element of arr initialised

There is also an alternate way to create and initialize the array together. It is good if you want to create and initialize the array at the same time.

int[] arr={1, 2, 3}

Here, the array arr is initialized with values 1, 2, and 3. The size of the array will be automatically calculated by counting the number of elements inside the curly braces.

Java Array Example

We can understand all of these from the below example:

public class Main
{
  public static void main(String[] args) {
  //declaring an array
  int[] arr;
  //creating an array
  arr=new int[5];
  //initializing array
  arr[0]=1;
  arr[1]=2;
  arr[2]=3;
  arr[3]=4;
  arr[4]=5;
  //another way to declare, create and initialize an array
  char[] arr2={'a', 'b', 'c'};
  //finding the length of array
  int len=arr.length;
  System.out.println("Elements of arr:");
  //accessing elements using for loop
  for(int i=0;i<len;i++)
  {
    System.out.print(arr[i]+" ");
  }
  System.out.println("\nElements of arr2:");
  //accessing elements using for loop
  for(int i=0;i<arr2.length;i++)
  {
    System.out.print(arr2[i]+" ");
  }
}
}

Output:

Elements of arr: 
1 2 3 4 5 
Elements of arr2:
a b c

In the above example, we have used the Java flow control statements (for loop) to print all elements of the array. We can also perform the same task using the for-each loop.

If you want to iterate array elements using the for-each loop then see the below example:

public class Main
{
  public static void main(String[] args) {

    char[] arr={'a', 'b', 'c'};
    //using for-each loop
    for(char i:arr)
    {
      System.out.println(i);
    }
  }
}

Output:

a
b
c

Advantages of Array

  • The array allows us to store multiple values in a single variable.
  • We can easily access the element of the array using its index value.
  • Arrays are faster as compared to the primitive data types.

Disadvantages of Array

  • They have a fixed size. Its size cannot be changed at runtime.
  • There are chances of memory wastage.

Conclusion

The array is a data structure that allows us to store multiple values in a single variable. Although array has some disadvantages but it is widely used for many purposes. So, it is always advised to learn and understand the concept of an array before applying.

Leave a Reply

Your email address will not be published. Required fields are marked *