Java Array

In this article, you will learn how to work array in Java. You will learn to declare, initialize and access array elements with the help of examples.

Array is used to store similar type of elements that have a continuous memory allocation.

Array is collection of multiple values in a single variable, instead of declaring separate variables for each value.

Java array is an object with fixed set of elements. We can store fixed set elements in array and if we try to store more elements with it's length then it should throw runtime exception ArrayIndexOutOfBoundsException in Java Array.

Java array is index-based and Java array index start with o index.

Advantages of Java Array

Java Array have many advantages:

  • Single storage: We can store multiple elements in single object instead of creating multiple variables for store values.
  • Easy Assess: We can get elements easily with Array object.
  • Random Access: We can get any data by using Array index.

Disadvantages of Java Array

Java Array also have disadvantages:

  • Java arrays are homogeneous: All elements of an array must be of the same type.
  • Size limit: We can store fixed size of elements in array basis on array length.
  • Memory dependancy: We can't increase or decrease length of array at runtime we have to create new array if we want to increase or decrease length of array. If we have array length of 5 but we have stored only 2 elements then rest 3 index is not available for other array.