BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Array explained with examples

Last Updated: June 11, 2024 by Chaitanya Singh | Filed Under: java

Array is a collection of elements of same type. For example an int array contains integer elements and a String array contains String elements. The elements of Array are stored in contiguous locations in the memory. Arrays in Java are based on zero-based index system, which means the first element is at index 0.

This is how an array looks like:

int number[] = new int[10]

Here number is the array name. The type of the array is integer, which means it can store integer values. The size of the array is 10.

Array works on an index-based system. In the above array, number[0] represents the first element of the array, number[1] represents the second element of the array and so on. The index of array starts from 0 and ends at array_size-1. In the above example, the index of first element is 0 and index of 10th element is 9.

Advantages of Array

Better performance: Since array works on a index based system, it is easier to search an element in the array, thus it gives better performance for various operations.

Multidimensional: Unlike ArrayList which is single dimensional, array are multidimensional such as 2D array, 3D array etc.

Faster access: Accessing an element is easy in array.

Disadvantages of Array:

Fixed Size: The size of the array is fixed, which cannot be increased later.

Allows only similar type elements: Arrays are homogeneous, they don’t allow different type values, for example an int array cannot hold string elements, similarly a String array cannot hold integer elements.

Insertion and delegation requires shifting of elements.

Declaration, Instantiation and Initialization of Array in Java

This is how we declare, instantiate and initialize an array. I have covered this in separate tutorial as well: Declaration and initialization of an Array.

int number[]; //array declaration
number[] = new int[10]; //array instantiation
number[0] = 10; //array Initialization
number[1] = 20; //array Initialization

We can also declare an array like this: All the three following syntax are valid for array declaration.

int[] number; 
int []number;  
int number[];

Example:
The following example demonstrates, how we declared an int array, initialized it with integers and print the elements of the array using for loop.
Note: You can see that we have used length property of array to find the size of the array. The length property of array returns the number of elements present in the array.

public class JavaExample{
  public static void main(String args[]){
    //array declaration, instantiation and initialization
    int number[] = {11, 22, 33, 44, 55};

    //print array elements
    //length property return the size of the array
    for(int i=0;i<number.length;i++)
      System.out.println("number["+i+"]: "+number[i]);
  }
}

Output: